Skip to content

Latest commit

 

History

History
115 lines (85 loc) · 2.65 KB

cookbook.org

File metadata and controls

115 lines (85 loc) · 2.65 KB

Recipes

Count lines in a file

awk 'END { print NR }'

Count no of words in a file

awk '{ total = total + NF } END { print total }'

Find sum of columns for each row

awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'

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

Remove duplicate lines from a file

awk '!a[$0]++' data.txt

Print all lines containing “Programming”

awk '/Programming/ { n++ }; END { print n+0 }'

Prefixing line with increasing number

awk '{print FNR ". " $0}' data.txt

Print account name and role from /etc/passwd

awk -F : '/^[^(#|_)]/ {print $1, "=>", $5}' /etc/passwd

Find the line containing largest value in the first column

awk '$1 > max {max=$1; maxline=$0}; END{ print maxline}' data.txt

Swap first two columns of every line

awk '{temp=$1; $1=$2; $2=temp} 1' data.txt

Delete second column from every line

awk '{ $2 = ""; print }' data.txt

Perform search similar to SQL ‘where’

awk '$2 == "hello"' data.txt

Delete leading whitespace from each line

awk '{sub(/^[ \t]+/, ""); print}'

Delete trailing whitespace from each line

awk '{sub(/[ \t]+$/, ""); print}'

Replace all instances of “foo” with “bar”

awk '{gsub(/foo/,"bar");print}'