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.

No comments:

Post a Comment