awk <command> <file1> <file2> ... <filen>
{}
can be separated with ;
-f progfile
awk -F',' '{print $1}' filename
awk -F"," -v OFS=";" '{print $2}' filename
simple match
awk '/J/ {print}' filename
match only field against regex (compared to whole line above)
awk -F"," '$1 ~ /J/ {print}' filename
// or !~
for not matching
Prints where snd field in line is > 200
awk -F"," '$2>200 {print $0}' filename
Multiple conditions can be &&
||
!
awk 'BEGIN { print "before first line" } { print $0 } END { print "After last line" }' filename
ls -l | awk 'BEGIN {sum=0} $5>200 {sum+=$5} END{print sum}'
{ print $0; } # prints $0. In this case, equivalent to 'print' alone
{ exit; } # ends the program
{ next; } # skips to the next line of input
{ a=$1; b=$0 } # variable assignment
{ c[$1] = $2 } # variable assignment (array)
{ if (BOOLEAN) { ACTION }
else if (BOOLEAN) { ACTION }
else { ACTION }
}
{ for (i=1; i<x; i++) { ACTION } }
{ for (item in c) { ACTION } }
BEGIN { # Can be modified by the user
FS = ","; # Field Separator
RS = "\n"; # Record Separator (lines)
OFS = " "; # Output Field Separator
ORS = "\n"; # Output Record Separator (lines)
# Set variables
Var = 22;
print "Running the script"
}
/J/ && Var == 22 {printArg($1)}
END {
print "Done with script"
}
function printArg(arg) { print arg; }