Read text file by condition with AWK

Posted on November 15, 2014, 8:55 pm


What is AWK ?

AWK (sometimes called nawk) is a Linux programming language designed by Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan.
The name of the language was taken from the three letters of the designer's name (Aho, Weinberger, and W. Kernighan).

Read text file by condition with AWK
Read text file by condition with AWK


Even the original version of awk was written in 1977 and continue their innovation to this day, but this language is still new for me :p and I found that it is more powerful, more practice and largely used in shell programming.
It is easy to discover another programming language if you've already known one isn't it ?
So now let's start some tutorial with me.
I have a text file like this :

Read text file by condition with AWK
Read text file by condition with AWK


Each fields are separated by a semi column and I want to get all the value of the field number 5 where field number 2 contain this value "15300"
So, I can do something like this :

cat YYYY-MM-DD-HH_MM_SS.csv | awk -F";" '{if($2 ~ /15300/)print $5;}'

-F ";" : split row by ";" and the result will put into a array (start from $1,$2....)

If you want more condition, do like this

cat YYYY-MM-DD-HH_MM_SS.csv | awk -F";" '{if($2 ~ /15300/ || /12700/ || /1800/) print $2;}' | sort

Sort : for sorting what you printed on the screen

Blog Archive

  • 2017-November (1)
  • 2017-June (1)
  • 2016-September (5)
  • 2016-August (3)
  • 2016-June (1)
  • 2015-September (1)
  • 2015-May (1)
  • 2015-February (2)
  • 2014-December (1)
  • 2014-November (2)
  • 2014-September (3)
  • 2014-August (1)
  • 2014-June (1)
  • 2014-May (1)
  • 2014-March (1)
  • 2013-December (2)
  • 2013-October (2)
  • 2013-July (1)
  • 2013-June (3)
  • 2013-May (4)
  • 2013-March (1)
  • 2012-December (2)
  • 2012-November (1)
  • 2012-October (1)
  • 2012-September (2)
  • 2012-August (2)
  • 2012-July (2)
  • 2012-June (1)
  • 2012-May (2)
  • 2012-March (2)
  • 2012-February (2)
  • 2012-January (5)
  • 2011-December (2)
  • 2011-November (4)
  • 2011-October (5)
  • 2011-September (2)
  • 2011-August (1)
  • 2011-June (2)
  • 2011-May (1)
  • 2011-April (9)
  • 2011-March (3)
  • 2011-February (12)
  • 2011-January (16)
  • 2010-December (17)
  • 2010-November (6)
  • 2010-October (10)
  • 2010-September (8)