Awk oneliners
id : kv2nx85zze
category : computer
blog : unixlinux
created : 08/23/10 - 11:06:50

  • Print all fields without last one :
# awk 'BEGIN{FS=OFS="<delimiter>"}{$NF=""; NF--; print}'

  • Print last field :
# awk -F '<delimiter>' {'print $NF'}

  • Count lines (emulates "wc -l") :
# awk 'END{print NR}'

  • Print the total number of fields ("words") in all lines :
# awk '{ total = total + NF }; END {print total}' <file>

  • Print the total number of lines that contain "pattern" :
# awk '/<pattern>/{n++}; END {print n+0}' <file>

  • Print the number of fields in each line, followed by the line :
# awk '{ print NF ":" $0 }'

  • Print every line with more than 4 fields :
# awk 'NF > 4'


Glossary
  • NF : Number of fields
Links