Process text faster with Awk and Sed

Sed comes from “stream editor”,
The name Awk is derived from the surnames of its three authors – Alfred Aho, Peter Weinberger and Brian Kernighan – and is a handy pattern matching programming language that is Turing-complete.

Sed can handle and quickly process arbitrarily long inputs – its processing capabilities are similar to the capabilities of the ed editor

1) Install Awk and Sed

sudo apt-cache search nawk/gawk
If found then go for installation of the same

sudo apt-get install gawk

else check for which gawk 

2)  Using Sed

The following Sed code globally replaces a given string with another in a text file:

$ sed ‘s/string1/string2/g’ textfile
If you want to save the original file contents to a new file and the changed version to the 
original file, you should use the -i option as 

$ sed -i.bak ‘s/three/3/g’ text 
The previous command replaces every occurrence of the word “three” with 
“3”, saves the output to the file “text” and keeps the original content 
inside “text.bak”. If you want to perform a case insensitive global 
search and replace, you should use the following command: 

$ sed -i.bak ‘s/three/3/gi’ text

3) Using Awk

By default, Awk splits input lines into fields based on whitespace (spaces and tabs). You can change the default behaviour by using the -F command line option and providing another character.

To print "Hello World" using awk go to the command line and type out the following: -

echo "" | awk '{print "Hello World!"}' 

The above is a more useful way to print hello world using command line.

4) A Sed example

Sed allows you to make changes in multiple lines. The following example changes the path of the Perl binary from “/usr/local/bin/perl” to “/usr/bin/perl” in multiple files:

$ sed ‘s/\/usr\/local\/bin\/perl/\/usr\/bin\/perl/g’ *.pl
Note that you have to use the escape character “\” in order to use the “/” character as a normal character. Always remember that once a line is read by Sed, the previous line is gone forever. '

5) An Awk example










No comments:

Post a Comment