Posted on November 15, 2014, 8:55 pm
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 |
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 |
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