awk

Syntax

awk '{pattern + action}' {filenames}

Commands

  • Find users with GID > 500: awk -F: '$3 > 500' /etc/passwd
  • Check for a string: awk '$1 ~/nagios/' /etc/passwd
  • Print lines containing dani, nico, pisu: awk '/dani|nico|pisu/' /etc/passwd
  • Print all lines between 500/502 pairs: awk '/500/, /502/' /etc/passwd
  • Print lines longer than 80 characters: awk 'length > 80' /etc/fstab
  • Print specific fields : ps aux | awk '{ print $8 " " $2 }' or awk -F: '{print $1,$5}' /etc/passwd
  • Print user and group for all files owned by myqsl user: ls -l /var/log | awk ' /mysql/ { print $3 " " $4 } '
  • List UID and GID separated by "-": awk -F':' 'BEGIN {OFS="-";} {print $3,$4;}' /etc/passwd
  • Display the number of line and the number of fields for that line: awk -F: '{print NR,"->",NF}' /etc/passwd
  • Print total number of lines that contain word Error: awk ' /Error/ {nlines = nlines + 1} END {print nlines}' /var/log/messages
  • Print all users if GID > UID: awk -F: '$4 > $3 {print $1}' /etc/passwd
  • Print all lines from /etc/passwd that has the same UID and GID: awk -F ':' '$3==$4' /etc/passwd
  • Print the number of the line and the first field: awk -F: '{print NR, $1}' /etc/passwd
  • Print every line after erasing the 2nd field: awk -F: '{$2 = ""; print}' /etc/passwd
  • Print echo 100 times: yes | head -100 | awk '{ print "echo" }'
  • Add up first column and print total sum: awk -F: '{s += $3} END { print "Total sum is", s}' /etc/passwd
  • List all files names whose file size greater than 810K: ls -al |awk '$5 > 828640 {print $9}'
  • List all file whose file size is greater than 810K and owner is "mysql": ls -al | awk '$3 == "mysql" && $5 > 9355 {print $9}'
  • List all the files whose owner is not "root": ls -al |awk '$3 != "root" {print $9}'
  • Display "D" processes: top -b -n 1 | awk '{if (NR <=7) print; else if ($8 == "D") {print; count++} } END {print "Total status D: "count}'
  • Change Maximum number of days between password change for all users with UID > 500: awk -F: '$3 >= 500 { system ("chage -M 180 " $1) }' /etc/passwd
  • Determine the total size of the JPG files in a particular directory: ls -l *.jpg | awk '{s+=$5} END {print "Total size: " s}'
  • Top 20 popular pages for a website: cat /var/log/httpd/access_log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  • List all users with bash shell: cat /etc/passwd | awk -F: '{if ($7 ~ /bash/) print $1;}'
  • List all users without a bash shell: cat /etc/passwd | awk -F: '{if ($7 !~ /bash/) print $1}'
  • Display all lines from Apache log file if HTTP error code is 404: awk '$9 == 404 { print $0}' /var/log/httpd/access_log
  • Print lines from the nth field: echo 'This is a test' | awk '{print substr($0, index($0,$3))}'
  • Sum up all *.conf files from /etc: for i inls -l *.conf | awk '{print $9}'; do du -sk $i; done | awk '{sum = sum + $i} END {print sum}'
  • Display the free memory+cache: free -m | awk 'NR==3 {print $4 " MB"}'