Bhimalapuram.co.in
AWK

Telugu SystemWithoutTelugu

AWK

Awk can complete very complicated work with few commands/few lines. It is invoked with the phrase "awk".It can be used for handling one file, and also for dealing with multiple files by way of "do ...done" loop.

Examples:

Disclaimer: Awk (rather awk- without capital letter- to be exact) has very precise syntax. Space omitted or unnecessary space, omission or unnecessary addition of punctuation marks/special characters will stop it from working or give wrong results. Check the syntax which you use minutely ......just to speed up your work.

"awk '/pattern/{print}'" will select items of that specified pattern. The command "awk '/5 warnings/{print $1}' mistakes.txt> five.txt" was used to select records which reported 5 warnings in mistakes.txt which had records showing 4 mistakes, records showing 3 mistakes etc. into a new file- five.txt.

"awk '{print}' ragalist.txt" will print the file "ragalist.txt". "awk '{print $2,$3,$5}' cities.txt> newcities.txt" will copy 2nd,3rd and 5th fields of the file cities.txt to the new file- newcities.txt with blank space as field seperator. "awk '{print $2,",",$3,","$4} cities.txt> newcities.txt" will copy from the file cities.txt- 2nd field then comma then 3rd field then comma then fourth field to the file newcities.txt.

The order of fields in records in a file can be changed as required. "awk -F '{print $4,",",$3,",",$1} cities.txt> newcities.txt" will give 4th, 3rd and 1st fields of the file cities.txt in the new file- newcities.txt.

To print a selected range of lines from a big file:: "awk 'NR>=34&&NR<=56'inputfile> newfile". This command copies 34 to 56 from the inputfile to newfile.

awk '{if(NR==69) print NR, $0}' inputfile> outputfile will print line 69. "for files in *; do awk '{if(NR==69) print NR, $0}' "$files"; done> result.txt"- is the command used to print only 69thline in all files in a directory to a file (to enable us to check if a required condition/phrase was present).

Please the documentation on awk to understand full power of awk - and to be able to use it fully.

Back To Directory English Telugu SystemWithoutTelugu

Valid XHTML 1.0 Transitional

Valid CSS!