AWK

Useful variables

Variable Description
FS Field seperator
RS Records seperator
NR nr of records
NF nr of fields
OFS output field seperator
ORS output record seperator

Split lines in file on delimiter after matching it to regexp

cat some_file.txt | awk -F ': ' '/^regexp/ {print $2 " - " $1}'

Match a colum

awk '$1 ~ /^regexp/ {print $2}'

Multiple matches

awk '/regexp1/ {print $1} /regexp2/ {print $2}'

Conditional statements

awk '{if($1 == "muh") && ($3 != "maeh") { print $2 } }'

Counter

cat some_file | awk 'BEGIN { x=1 } { print x; x += 1 }'
  • Or just use the variable NR the line counter (counts all lines not only matching ones)

Sum up a column

cat some_file.txt| awk '{ sum+=$3} END {print sum}'

String replacement

cat some_file.txt | awk 'sub(/regexp/, "replace", $1)'

Defining external variables / Limiting

cat some_file.txt| awk -v LIMIT=30 '{a=$1; sub(/user/, "", a); if(a <= LIMIT){print}}'