Difference between revisions of "Linux command: awk"
Jump to navigation
Jump to search
Rafahsolis (talk | contribs) |
Rafahsolis (talk | contribs) |
||
| Line 19: | Line 19: | ||
== Print with condition == | == Print with condition == | ||
awk '{if ($3 =="" || $4 == "" || $5 == "") print "Some score for the student",$1,"is missing";'}' student-marks | awk '{if ($3 =="" || $4 == "" || $5 == "") print "Some score for the student",$1,"is missing";'}' student-marks | ||
| + | |||
| + | == Print if value == | ||
| + | <nowiki>$ echo "a b c d" | awk '($4){print "yes"}' | ||
| + | yes | ||
| + | $ echo "a b c d" | awk '($14){print "yes"}' ## prints nothing, no $14 | ||
| + | $ echo "a b c 0" | awk '($4){print "yes"}' ## prints nothing, $4 is 0</nowiki> | ||
== Print columns as lines == | == Print columns as lines == | ||
Revision as of 17:11, 7 January 2018
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
Print if value
$ echo "a b c d" | awk '($4){print "yes"}'
yes
$ echo "a b c d" | awk '($14){print "yes"}' ## prints nothing, no $14
$ echo "a b c 0" | awk '($4){print "yes"}' ## prints nothing, $4 is 0
Print columns as lines
ls -lR | awk '{for(x=1;$x;++x) print $x}'
awk '{for(x=1;$x;x++)print $x}'
___ __ ___
| | |
| | |-----> increment x by 1 at the end of each loop.
| |--------> run the loop as long as there is a field number x
|------------> initialize x to 1