AWK Cookbook Kiran Gangadharan
Contents
1 Recipes 1
1.1 Countlinesinafile........................ 1
1.2 Countnoofwordsinafile.................... 1
1.3 Findsumofcolumnsforeachrow ............... 1
1.4 Findandreplaceinfile...................... 2
1.5 Removeduplicatelinesfromafile................ 2
1.6 Printalllinescontaining"Programming". . . . . . . . . . . . 2
1.7 Prefixinglinewithincreasingnumber. . . . . . . . . . . . . . 2
1.8 Print account name and role from /etc/passwd . . . . . . . . 2
1.9 Find the line containing largest value in the first column . . . 2
1.10Swapfirsttwocolumnsofeveryline .............. 2
1.11 Delete second column from every line . . . . . . . .
1.12 Perform search similar to SQL ’where’ . . . . . . .
1.13 Delete leading whitespace from each line . . . . . .
1.14 Delete trailing whitespace from each line . . . . . .
1.15 Replace all instances of "foo" with "bar" . . . . . .
1 Recipes
1.1 Count lines in a file
awk ’END { print NR }’
1.2 Count no of words in a file
awk ’{ total = total + NF } END { print total }’
1.3 Find sum of columns for each row
awk ’{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}’
1.4 Find and replace in file
# Replace only first instance in line
awk ’{sub(/HTML/, "html")}; 1’ data.txt > data_new.txt # Replace all instances in line
awk ’{gsub(/HTML/, "html")}; 1’ data.txt > data_new.txt
1.5 Remove duplicate lines from a file
awk ’!a[$0]++’ data.txt
1.6 Print all lines containing "Programming"
awk ’/Programming/ { n++ }; END { print n+0 }’
1.7 Prefixing line with increasing number
awk ’{print FNR ". " $0}’ data.txt
1.8 Print account name and role from /etc/passwd
awk -F : ’/^[^(#|_)]/ {print $1, "=>", $5}’ /etc/passwd
1.9 Find the line containing largest value in the first column
awk ’$1 > max {max=$1; maxline=$0}; END{ print maxline}’ data.txt
1.10 Swap first two columns of every line
awk ’{temp=$1; $1=$2; $2=temp} 1’ data.txt 2
1.11 Delete second column from every line
awk ’{ $2 = ""; print }’ data.txt
1.12 Perform search similar to SQL ’where’
awk ’$2 == "hello"’ data.txt
1.13 Delete leading whitespace from each line
awk ’{sub(/^[ \t]+/, ""); print}’
1.14 Delete trailing whitespace from each line
awk ’{sub(/[ \t]+$/, ""); print}’
1.15 Replace all instances of "foo" with "bar"
awk ’{gsub(/foo/,"bar");print}’
网友评论