Sep 1, 2010

Sed & awk

We can use sed to search for some string patterns. By using sed we can easily modify a string.

Search and replace
The basic command is like this:
sed 's/day/night/' output_fie
This code will search for the keyword 'day'in the input_file and replace it by night then save the changed file in output_file.

sed 's/\(day\).*/\1' output_file
This will search for the day keyword in the input file and it will delete other characters behind the keyword, then it save the data in output_file.

We can use awk for report writing and filtering. Mainly it is a good table creator.

A bacsic structure of a awk command
BEGIN { print "START" }
{ print }
END { print "STOP" }

Try this out;
#!/bin/csh -f
awk '
BEGIN {print "FILE\tOWNER" } \
{ print $8 "\t" $3 }\
END { print "--DONE--"}\ '

This will give the output as

Note :
In awk "$2" is not a variable. It always reffer to a column number.
There are three types of awk.awk, nawk and gawk.

If you want to use a variable use it as,
and in the code;

No comments:

Post a Comment

Your comments are always welcome ...