awk is mini language for text processing . could be useful when filtering columns or rows
sample text file
[root@localhost home]# cat awk.txt
python, dynamic, webapp, medium
php, dynamic, webapp,easy
c, static, embed,hard
go, static, webapp, medium
filter row
[root@localhost home]# awk /python/’{print $1, $2, $3, $4}’ awk.txt
python, dynamic, webapp, medium
filter column
[root@localhost home]# awk -F, /python/’{print $1, $2}’ awk.txt
python dynamic
here this using ‘,’ as field separator (because default is space)
other parameters
NF — Number of Fields: total fields in the current line
NR — Number of Records: total records processed
RS — Record Separator: delimiter for records (default is a new line)
OFS — Output Field Separator: separator for fields’ output (default is one whitespace)
ORS — Output Record Separator: separator for records’ output (default is a new line)
use different separator for password file
awk -F: ‘{print $1}’ /etc/passwd
root
bin
daemon
…
use begin+end print table format
[root@localhost home]# awk -F, ‘BEGIN {printf("%s %8s %15s %20s \n", “languagage”, “type”,”app”,”difficulty”)} {printf(“%5s %15s %15s %15s\n”, $1, $2, $3, $4)} END {printf (“total rows %5s\n”, NF)}’ awk.txt
languagage type app difficulty
python dynamic webapp medium
php dynamic webapp easy
c static embed hard
go static webapp medium
total rows 4
condition filtering
using if statement
[root@localhost home]# awk -F, ‘{if ($4 ==”easy” ) {printf “%s %s %s %s\n”, $1 ,$2, $3, $4} }’ awk.txt
php dynamic webapp easy
or use ~
[root@localhost home]# awk -F, ‘$4 ~ /easy|medium/ {printf “%s %s %s %s\n”, $1 ,$2, $3, $4} ‘ awk.txt
python dynamic webapp medium
php dynamic webapp easy
go static webapp medium[root@localhost home]# awk -F, ‘$4 ~ /medium/ {printf “%s %s %s %s\n”, $1 ,$2, $3, $4} ‘ awk.txt
python dynamic webapp medium
go static webapp medium
using loop + condition
[root@localhost home]# awk -F, ‘{for (i=1;i<=NF;i++){if ($i ~/hard/) {printf “%s is very difficult \n”, $1} } }’ awk.txt
c is very difficult[root@localhost home]# awk -F, ‘BEGIN{total=0;} {for (i=1;i<=NF;i++){if ($i ~/hard|medium/) {total+=1 } } } END{printf “medium or hard: %s\n”,total} ‘ awk.txt
medium or hard: 3