Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Tuesday, March 1, 2011

Using SED tool to manipulate the files

Dear geeks,

Here I come with the topic how to manipulate the files by using SED and AWK tool. Before getting into actual topic we will have a look at about the history of these tools.

SED:

SED is often called as stream editor. Because it reads the input file lines by line. It was developed by BELL LABS.

Using this SED we can perform multiple tasks in a single command. It also REGEX compatible.

SYNTAX:

sed [option] 'instruction' input file

Where
[option] - is unix command line options.
instruction - Give the instruction which going to be performed

1.Getting the input based on the line numbers.

a).sed -n '1p' /var/log/messages

It will elaborate the first line from the given file.

b).sed -n '1,5p' /var/log/messages

Giving the 1 to 5 lines from the given file.

c). sed -n '1,5!p' /var/log/messages

Output will be all lines except first five lines.For that we have to issue the "!" symbol. It will relatively enumerate the files

d) sed -ne '$p' /var/log/messages

It prints the last printable line of the given input file.

So far we have seen how to manipulate the file based on the line number. Coming tutorial we will have a deep look into about how to use the REGEXES on SED tool. It will give more granular control than the normal manipulation.

RegEX_Tutorial_Part_1

Hi geeks,

Today we are going to start with the RegEX features in UNIX-LIKE operating systems. Basically RegEX always POSIX compatible. Almost all programming languages supports the RegEX features. Here we start with how to manipulate the texts through BASH SHELL by using some basic commands like grep,sed,awk with RegEX.

For following illustration i am going to use the file , which contains from 1 to 1000.

For creating the file from 1 to 1000 give the following command to your terminal.

seq 1000 > thousand.txt

it will create the file which contains 1 to 1000.

1.Getting the output from based on the first string.

grep '^9' thousand.txt

The symbol "^" enables you to getting the lines which contains 9 as a first letter.


2.Getting the output from based on the last string.

grep '9$' thousand.txt

The symbol "$" enables you to getting the lines which contains 9 as a last letter.

NOTE: The "$" symbol should be placed after the string which you want to find it out.

3.Getting the output by using the neta-character (.) .

For example you want to iterate the computer that you want to manipulate the strings by any match but not at the first field. The first field always should be 9. And after 9 two string may any numbers. For that the following command will help you to search.

grep '^9..' thousand.txt

it will give you all numbers which contains 9 as the first letter and 2-nd and 3-rd numbers with any match.

grep '^9.9' thousand.txt

It will give you the numbers which contains 9 as the first number and also the last number as 9 and middle string with any match.


Likewise you can manipulate your files by inter-changing the RegEX notations. The above prototypes only will help you guys to understand what it will do while giving these input to your computer. But to manipulate the different files with different RegEX ,we have to apply rational thoughts. As this is my first article in blogs I could not able to draft it clearly. Soon I will come with new enhanced topic about RegEx with more practical explanations.