Here I will discuss some scripting languages which I am familiar with. I generally do stuff with sed and awk when it is simple and use perl when I find that the task is complex. I start with grep which is a quite powerful tool as far as searching is concerned. Note that at the beginning of code > is not the part of code.
grep is a very powerful tool for searching patterns. If we want to print all the lines from a file named " file.txt " which have the word " foo " we use the following:
> grep "foo" file.txt
sed is an acronym for stream-editor. It can be used to do cool things with a string of characters, which may come from a file or from the terminal output of a linux command or an executable. The main things for which people use sed are as follows:
> echo "sed is hot " | sed s/hot/cool/
replaces " hot " by " cool " . In the above " s " stands for substitution. If we want to replace " hot " by " cool " in a file named " file.txt " we use the following:
> cat file.txt | sed s/hot/cool/
Note that for global replacement (replacing everywhere) we use the option " g "
> echo "sed is hot " | sed s/hot/cool/g
Note that the above commands will write the output on the terminal without changing the input file. However, we can direct the output in a new file:
> cat file.txt | sed s/hot/cool/g > file1.txt
will replace everywhere " hot " by " cool " in the file " file.txt " and will write output in a new file " file1.txt " .
There is a particular way for writing patterns and is universally followed by almost all scripting languages. The patterns written in this way are called " regular expression " or regexp . A regexp starts and end with a delimiter. In this tutorial we will be using " / " delimiters. I have a file called " mb.js " which I can see using Linux command " ls " . I want to see " mc " in place of " mb " for which I do the following:
> ls -l *.* | sed s/mb/mc/
If we use sed with option " -n " it does not print anything
> cat file.txt | sed -n s/hot/cool/
however, if we use the option " p " it prints only the lines in which the pattern was found
> cat file.txt | sed -n s/hot/cool/p
In order to delete the word " foo " from a file " file.txt " we use the following:
> cat file.txt | sed /foo/d
please note that " cat " is not a part of sed , it is a linux command to see the content of a file without opening it in an editor like " vi " and " | " is called " pipe " which is used to combine more than one linux commands.
awk is an amazing tool which can be used to manipulate string of characters which may be in the form of content of a file or terminal output. Some of the most interesting things about awk are that it recognizes rows and columns in a file and is compatible with the syntax of the c programming. In order to print the third column of a multicolum file named " file.txt " we use the following:
> cat file.txt | awk '{print $3}'or
> cat file.txt | awk '{printf(" %s\n",$3)}'
note that here we have used the syntax of c which makes awk very powerful. For example, we can print the average of third and four columns using the following:
> cat file.txt | awk '{printf(" %2.6f\n",($3+$4)/2)}'
using " $ " we can get any we wish and do things with that, however, there is a way to address rows also. For that there is a key work " NR ". This following piece of code will not print the third column of the file, it will print line (row) numbers also
> cat file.txt | awk '{printf(" %d %s\n ",NR,$3)}'
sometime we can do great things by conditional use of awk. For example, the following piece of code prints the square toot of the third column when it is positive
> cat file.txt | awk '{if($3 > 0.0) printf(" %2.6f \n ",sqrt($3))}'
between single quotes we can embed a piece of c and can get input for that by using " $ "
One interesting thing to note is that using pipe " | " we can join codes together. For example, the following piece of code uses both sed and awk.
> cat file.txt | sed -n s/hot/cool/p | awk '{printf(" %s\n",$3)}'
perl is a very flexible scripting language. It can be used to manipulate strings, accommodates many features of c programming, and by mixing linux commands and using external executable can do amazing things. The " hello world " of perl is as follows:
#!/usr/bin/perl print "Hello World ! \n";
write the above program in a file and name it something.pl (hello.pl) and run it as follows:
> perl hello.pl
if it prints " Hello World ! " on terminal you are successful otherwise check that weather perl is properly installed on your computer or not.
Every perl program must start with " #!/usr/bin/perl & . Apart from the above way, we can run a perl program by making it executable.
> chmod a+x hello.pl
> ./hello.pl