awk, simple but efficient text manipulation tool.
October 17th, 2006 mysurface Posted in Text Manipulation, awk | Hits: 6167 |
Awk is a simple and efficient for text manipulation, it is use to extract info from text data files especially the data is in the table form with columns and rows and each fields (columns) is separate by a common separator (usually a blank space).
awk '/keyword/' data.txt
It does the same as grep like bellow:
grep "keyword" data.txt
But what if I wanna print out only the 3rd and 4th columns if the rows match the keyword?
awk '/keyword/ {print $3 " " $4}' data.txt
Bare in mind, the keyword matching is at any columns, what if you want to match the keyword only in first column?
awk '$1 ~ /keyword/ {print $3 " " $4}' data.txt
Again it matches the word “keyword” and also other like “mykeyword” “keywords”, I want it matches only exactly “keyword”.
awk '$1 ~ /^keyword$/ {print $3 " " $4}' data.txt
or
awk '{ if ($1 == "keyword") print $3 " " $4;}' data.txt
There are a lots more to discover, check out this tutorial, I find it very simple to follow.
Have Fun!
Live Chat!









June 25th, 2007 at 2:34 am
[...] grep is always a useful tools to help analyze logs. When the logs are managed nicely in rows and columns format, awk will be much more efficient compared to grep. Refers to simple examples of awk here. [...]
June 25th, 2007 at 4:20 am
[...] grep is always a useful tools to help analyze logs. When the logs are managed nicely in rows and columns format, awk will be much more efficient compared to grep. Refers to simple examples of awk here. [...]