AWK & SED Reference

AWK Basics

# Structure: awk 'pattern { action }' file # Print specific columns awk '{print $1, $3}' file.txt # Field separator awk -F: '{print $1}' /etc/passwd awk -F',' '{print $2}' data.csv # Pattern matching awk '/error/ {print}' logfile awk '$3 > 100 {print $1, $3}' data.txt # BEGIN and END blocks awk 'BEGIN{sum=0} {sum+=$1} END{print "Total:", sum}' nums.txt # Built-in variables # NR = line number, NF = number of fields # FS = field sep, OFS = output field sep awk 'NR==5' file # print line 5 awk 'NF==3' file # lines with 3 fields

AWK Real Examples

# Sum column 2 awk '{sum+=$2} END{print sum}' data.txt # Count occurrences awk '{count[$1]++} END{for(k in count) print k, count[k]}' log.txt # Print between patterns awk '/START/,/END/' file.txt # Reformat CSV awk -F',' 'BEGIN{OFS="\t"} {print $2,$1,$3}' input.csv

SED Basics

# Substitute first occurrence per line sed 's/old/new/' file.txt # Global substitute sed 's/old/new/g' file.txt # In-place edit (backup) sed -i.bak 's/http:/https:/g' config.txt # Delete lines matching pattern sed '/^#/d' file.txt # remove comments sed '/^$/d' file.txt # remove empty lines # Print line numbers sed -n '5p' file.txt # print line 5 sed -n '5,10p' file.txt # print lines 5-10 # Insert/append sed '3i\new line' file # insert before line 3 sed '3a\new line' file # append after line 3