awk examples

awk '{print NR, $0}' filename
awk '{$1=NR; print}' filename
awk '{print $1,$2}' fielname
awk '{print $NF}' filename
awk 'NF>0{print $0}' filename
awk 'NF>4{print $0}' filename
awk '/test.*/{print $0}'  filename
awk '$1 ~ /^print.*/{print $0}' filename
awk 'BEGIN{sum=0}{sum+=$2}END{print sum}' filename
awk '{sum=0; for(i=1;i<=NF;i++)sum+=$i; print sum}' filename
awk '{n = split($0, array); print array[1], array[3]} ' filename 
awk '{x[NR]=$0} END{for(i=NR;i>0;i--)print x[i]}' filename 
awk '{amount[$1]=$2} END{for(name in amount) print name, amount[name]}' filename

Comments

comments powered by Disqus