Awk Scripting

Mar 25, 2018 18:30 · 556 words · 3 minute read Awk

Common bash awk commands

awk -F':' '{ print $1 }' /etc/passwd       # Change Field Separator
awk '{ print }' /etc/passwd                # Print a whole file
awk '/stan|kyle/' /etc/passwd              # 'OR' logic string contains
awk '/stan|kyle/ { print $1 }' /etc/passwd # Match and print
ls -la | awk '{ print $9, "U:" $3 }'       # Add text in between fields

Print awk in a nice table

ls -la | awk '{ printf "%-25s %s\n", $9, $3}'
ls -la | awk '{ printf "%s:%-5s %-5s %2s\n", $3, $4, $5, $9 }'

Formating

  • %i or d –Decimal
  • %o –Octal
  • %x –hex
  • %c –ASCII number character
  • %s –String
  • %f –floating number
  • -n –Pad n spaces on right hand side of a column.
  • n –Pad n spaces on left hand side of a column.
  • .m –Add zeros on left side.
  • -n.m –Pad n spaces right hand side and add m zeros before that number.
  • n.m –Pad n spaces left hand side and add m zeros before that.

Full awk scripting

Awk in 20 mins

BEGIN {
  # Can be edited by user
  #FS = ",";   # Field Separator
  #RS = "\n";  # Record Separator (lines)
  #OFS = " ";  # Output Field Separator
  #ORS = "\n"; # Output Record Separator (lines)

  # Can't be modified by the user
  #NF          # Number of Fields in the current Record (line)
  #NR          # Number of Records seen so far
  #ARGV / ARGC # Script Arguments

  if (threshold == "") {
    threshold = 1000 # default mailbox size
  }
  procs = 0 # are we in the =procs entries?
  print "MESSAGE QUEUE LENGTH: CURRENT FUNCTION"
  print "======================================"
}  # begin section

{
  # Only bother with the =proc: entries. Anything else is useless.
  procs == 0 && /^=proc/ { procs = 1 } # entering the =procs entries
  procs == 1 && /^=/ && !/^=proc/ { exit 0 } # we're done


  # Message queue length: 1210
  # 1       2     3       4
  /^Message queue length: / && $4 >= threshold { flag=1; ct=$4 }
  /^Message queue length: / && $4 <  threshold { flag=0 }

  # Program counter: 0x00007f5fb8cb2238 (io:wait_io_mon_reply/2 + 56)
  # 1       2        3                  4                       5 6
  flag == 1 && /^Program counter: / { print ct ":", substr($4,2) }
}        # loop section

END{}     # end section

# Call a function
{ somecall($2) }

# function arguments are call-by-value
function name(parameter-list) {
  ACTIONS; # same actions as usual
}

# return is a valid keyword
function add1(val) {
  return val+1;
}

# Available actions
{ print $0; }  # prints $0. In this case, equivalent to 'print' alone
{ exit; }      # ends the program
{ next; }      # skips to the next line of input
{ a=$1; b=$0 } # variable assignment
{ c[$1] = $2 } # variable assignment (array)

{ if (BOOLEAN) { ACTION }
  else if (BOOLEAN) { ACTION }
  else { ACTION }
}
{ for (i=1; i<x; i++) { ACTION } }
{ for (item in c) { ACTION } }

# Patterns
/admin/ { ... }     # any line that contains 'admin'
/^admin/ { ... }    # lines that begin with 'admin'
/admin$/ { ... }    # lines that end with 'admin'
/^[0-9.]+ / { ... } # lines beginning with series of numbers and periods
/(POST|PUT|DELETE)/ # lines that contain specific HTTP verbs