Saturday, July 21, 2012

Six Simple And Awesome Awk One Liners for Unix Interviews


Six Simple And Awesome Awk One Liners for Unix Interviews

Awk is a little programming language, with a syntax close to C in many aspects. It is an interpreted language and the awk interpreter processes the instructions. Today I present to you some tricks which can Make our lives really easy in Unix.

1.To Negate some lines with AWK:

 These are called awk one liners. The First trick for today is how to negate some rows while using awk.
The Trick is : awk -F ” ” ‘NR>5 && NR<7{print $1,$2,$3}’ 1.txt
Note : This question will mostly asked in interviews where you will be asked to print some parameters from a file mostly a log file where in you will be asked to negate some rows herein refered to as records in awk. The above command here performs the following action. The field separator mostly called as delimiter here will be a space and then it will negate all the records and will only iterate through line numbers five through seven and then print the first second and third parameter.

2. To Take only a few Lines:

How would you print only lines match between Rahul and Abhishek using awk? Considering your file names.txt contains :
Rahul
Sahil
Geetika
Mamta
Abhishek
and the expected output should be like :
Sahil
Geetika
Mamta
The Trick is : $ awk ‘/Rahul/{ P=1; next } /Abhishek/ {exit} P’ names.txt

3. Context Addressing in AWK:

The above command will give you the desired output. Just try. If the Interviewer asks you what is this called, Just say this is called the CONTEXT addressing in awk. The interviewer might also be interested in asking you to print a certain range of lines from a file where in you do not have a pattern defined. We all know awk works on the principle of condition { actions }. Where condition is typically an expression and action is a series of commands. The input is split into records, where by default records are separated by newline characters so that the input is split into lines. So we know the context addressing can be used just like grep, so here you go.
The Trick is : awk ‘$0 ~ /pattern/ {print $0}’

4. Taking a range of records:

Suppose one wants to print all the lines in a file that match some pattern, the above code should be used.Lets suppose he wants you to print logs from line number 15000 to 16000.Just give him a simple blunt reply.
The Trick is  : head -16000 filename | tail -1000.

5. Deleting only files :

Now the interviewer might be tempted to ask you to delete all the files in the current directory with awk.
The Trick is  : ls -l|awk ‘$1!~/^drwx/{print $9}’|xargs rm

6. Removing Duplicates without sorting :

Be careful when trying this out in your home directory. This will remove all the files. awk syntax is not the same in every Unix system, but there is a way to learning how is it in our particular system: man awk. Now lets see our last trick, this talks about deleting the duplicate entries in file without sorting. There are thousand ways of doing one thing in unix lets see a couple of them. Suppose your file text.txt has contents the below contents.
/users/env> cat test.txt
abcd
efgh
ijkl
mnop
abcd
/users/env > cat test.txt |awk !’x[$0]++’
abcd
efgh
ijkl
mnop
Now this one lines removes the duplicate without sorting them. Enjoy Playing with awk.

No comments:

Post a Comment