Just some stuff about me.
Here's my dotfiles repository.
What links here:
https://backreference.org/2010/02/10/idiomatic-awk/index.html
/pattern/
is the same as $0 ~ /pattern/
$0
, which is whole linedefault action is print
awk '/pattern/'
prints every line matching the patternawk 1
prints every lineyou can use awk for substitution, the 1 prints the line:
awk '{sub(/pattern/, "foobar")} 1'
exclude empty lines (where NF
, number of fields, is 0):
awk 'NF'
remove last field:
awk 'NF--'
prepend line numbers (yes you can just use nl
instead):
awk '$0 = NR" "$0'
deduplicate lines:
awk '!a[$0]++'
print in 5 columns:
awk 'ORS = NR % 5 ? FS : RS'
replacing delimiters: the important thing here is that $0
doesn’t get recomputed unless AWK thinks it has to be. and that happens when a field is changed.
awk FS=\; OFS=, '$1=$1'
Also, any files that have an equal sign get treated as variables. Consequently, if yoru filename has an equal sign, you’re gonna have a bad time.