Difference between revisions of "Linux command: awk"

From RHS Wiki
Jump to navigation Jump to search
Line 17: Line 17:
 
       printf("\n") }'
 
       printf("\n") }'
 
</source>
 
</source>
 +
== Print with condition ==
 +
awk '{if ($3 =="" || $4 == "" || $5 == "") print "Some score for the student",$1,"is missing";'}' student-marks

Revision as of 13:26, 17 April 2016

Comand line text processing Examples:

awk -F, '{print NR, length($0)}' filename.txt  #print line number and line length
awk '{print FILENAME " " length($0)}' */PRF* | uniq

awk 'BEGIN { FS = "," } ; { print $2 }'  #Specify separator ',' can be done with -F too.
awk -F"," '$2~/^ABC$/' file  #Find in a csv second field = ABC

Print from 3rd field till end

awk '{              \
       for (i = 3; i <= NF; i++) {   \
          printf("%s ", $i);         \
       }                             \
       printf("\n") }'

Print with condition

awk '{if ($3 =="" || $4 == "" || $5 == "") print "Some score for the student",$1,"is missing";'}' student-marks