2. awk command examples for condition matching.
a. awk '$1 "~"/Anshul/ {print $0}' students
Explanation:
—> $1 : Use first column for basis of further action
—> ~ (tilde sign) : Relationship Operator which will Match string i.e. /Anshul/
—> {print $0} : print all possible fields i.e. entire rec
So the output of the above awk command example it to print all the records with all the fields having first column as Anshul.
[root@kalwa1 tmp]# awk '$1 ~ /Anshul/ {print $0}' students Anshul 279 555-1112 anshul 2 [root@kalwa1 tmp]#
b. How many students had a value in the 5th field >= 10
Example:
[root@kalwa1 tmp]# awk '$5 >= 10 {print $0}' students Atul 280 555-4221 atul 10 Romel 284 555-2121 shrikant 12 [root@kalwa1 tmp]#
c. How many students had a value in the 5th field < 10 and also had second field greater than 280.
[root@kalwa1 tmp]# awk '$5 < 10 && $2 > 280 {print $0}' students Manmohan 286 555-5574 manmohan 8 [root@kalwa1 tmp]#
Leave a Reply