Format output

Align output in fixed-width columns with printf

awk '{printf "%-20s %5d\n", $1, $2}' data.txt

tool
awk15 commands
tags
awklinux

More awk commands

  • awk -f transform.awk input.txt

    Load the awk program from a separate file

  • awk '{gsub(/foo/, "bar"); print}' input.txt > tmp && mv tmp input.txt

    Replace all matches and write the result back to the same file

  • gawk -v FPAT='([^,]+)|("[^"]+")' '{print $2}' data.csv

    Parse quoted CSV fields correctly with FPAT in gawk

  • awk 'BEGIN{print "Start"} {lines++} END{print lines" lines processed"}' file.txt

    Run code before and after all lines are processed

  • awk 'NR==FNR{a[$1]=$2; next} $1 in a {print $0, a[$1]}' lookup.txt data.txt

    Join data.txt with lookup.txt on the first field; lines without a match are skipped

all 15 awk commands