awk, simple but efficient text manipulation tool.

October 17th, 2006 mysurface Posted in awk, Text Manipulation | Hits: 25460 | 3 Comments »

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!

3 Responses to “awk, simple but efficient text manipulation tool.”

  1. [...] 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. [...]

  2. [...] 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. [...]

  3. This website has got some really helpful info on it. Cheers for helping me!

Leave a Reply