Two-file join on key

Left-join data.txt with lookup.txt on first field

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

tool
awk15 commands
tags
awklinux

More awk commands

  • awk '{print $3}' file.txt

    Print the 3rd whitespace-delimited field

  • awk -F: '{print $1, $3}' /etc/passwd

    Use colon as field separator, print user and UID

  • awk '/ERROR/ {print NR": "$0}' app.log

    Print line number and content for lines matching ERROR

  • awk '{sum += $4} END {print sum}' access.log

    Accumulate a numeric column and print total

  • awk '/START/,/END/' file.txt

    Range pattern: include lines from START to END inclusive

all 15 awk commands