awk 

Send to Kindle
home » snippets » linux » awk



Sample

matches = ($0 ~ pattern)

fcount += matches    # 1 or 0

 if (! matches)
     next

Samples from history

# Print sorted unique values of the 2nd column in a CSV file.
awk -F ',' '{ print $2 }' < input.txt | sort | uniq

ls -l | awk '/[[:digit:]]+\/ad.sst$/ { match($0, /([[:digit:]]+)\/filename.ext$/); print substr($0, m[1, "start"], m[1, "length"]);  } '
ls -l | awk '/[[:digit:]]+\/ad.sst$/ { match(); print substr($0, m[1, "start"], m[1, "length"]);  } '


du --max-depth=1 -h -x -L . | awk '/.*G\t/ { print $0  }'


lsof -F p -i TCP:8080 | awk '/p(.+)/ { print substr($1,2) } ' | xargs -P 8 kill

lsof -F p -i TCP:8080 | awk '/p(.+)/ { print substr($1,2) } ' | xargs -P 8 -n 1 cmd_that_takes_only_1_param

# Extract a hostname follwing a Host: in a commands output.
# e.g.  If there's exactly one line "Host: www.google.com" -> HOSTNAME="www.google.com"
HOSTNAME=$(command_printing_a_host_line | awk '{ match($0, /^Host:[[:space:]]+(.*)/, groups); if (groups[1]) print groups[1] }')