Awk Notes:
default
# Awky Mawky Nawky Gawky, you can awk me, you can nawk me,
# heck you can even mawk me, though don't you ever gawk me?
# awk program structure:
# pattern { action }
# ...
awk '{print}' test-files/list.txt
awk '{print $0}' test-files/list.txt
# Print each line of file
awk '{print $1}' test-files/list.txt
# Print first column of each line in list.txt
awk '{print $1,$2}' test-files/list.txt
# Print 1st and 2nd column
awk '{print $1$2}' test-files/list.txt
# Print 1st and 2nd column, without a space between the two
awk '/copy/ {print}' test-files/list.txt
# Print each line that has the word "copy" in it
awk '/[a-z]/ {print}' test-files/list.txt
# Print every line that contains at least one lowercase letter
awk '/[0-9]/ {print}' test-files/list.txt
# Print everyline that contains at least one number
awk '/^[0-9]/ {print}' test-files/list.txt
# Print lines that start with a number
awk '/[0-9]$/ {print}' test-files/list.txt
# Print lines that end with a number
awk '{ if($1 ~ /123/) print}' test-files/list.txt
# Print lines that 1st column == 123
cat /etc/passwd | awk -F: '{print $1}'
# Display all users of system
# -F: = changes field seperator to : (rather than a space)
echo 'yummy kitties hiss wildly' | awk '{gsub(/kitties/,"girls")}{gsub(/hiss/,"scream")};1'
# Double gsub
awk '{ print }' test.txt
awk '{ print $0}' test.txt
# Prints out test.txt file
# $0 prints the entire line (all fields)
awk '/[Mm]ystic/' | sort
# Search for mystic or Mystic and sort the output
ls *.js | awk '{print "cp "$0" "$0".bak"}' | bash
# Back up javascript files (replace bash with your shell)
cat /tmp/test | awk '$0!~/^$/ {print $0}' > /tmp/test1
# Delete blank lines
awk '/^$/ { print "blank line" } ' 2H\ Weapon
# Print blank line for every line
ps ax | grep -i gimp | awk '{print "kill -9 " $1}' | bash
# Kill all instances of gimp
gawk -v IGNORECASE=1 '/murderous/' 2H\ Weapon
# gawk can be case insensitve, -v set a variable from command line
awk '/find this text/ { print $0 }' in-this-file.txt
# Example awk program on cmd-line
# $0 = current line (every column in line)
# This will print any line containing "find this text"
awk '/find this text/' test-files/in-this-file.txt
# If you skip the {action} awk defaults to printing each line
awk '/find this text/ { print }' in-this-file.txt
# $0 could also be skipped and just use {print} to spit out each line
awk 'program' input-file-uno input-file-mas ...
awk 'pattern {action}' input-file another-file ...
# All on command line:
awk -f awk-source.awk text-file.txt this-file-too
#Running awk from an awk file fav-awk-program.awk:
# -f = file awk program is stored in (source file)
awk 'BEGIN { print "Mamma mia!" }'
awk "BEGIN { print \"Mamma mia! \" }"
# awken without an input file
awk 'BEGIN { print "Holy moly single quote is roley '"'"' poley" }'
awk 'BEGIN { print "Holy moly single quote is roley '\'' poley" }'
# Warning avoid quote sHELL by scurying to a file with -F, only the righteous should proceed:
awk 'BEGIN { print "You must masta <\47> technique to escape the single choke quote sequence" }'
awk 'BEGIN {print "Once you masta \47 you must practice \42 double choke quote sequence" }'
# Else you must master escape choke sequences in the octalgon:
# \042 and \42 = Double choke (quote) sequence
# \047 and \47 = Single choke (quote) sequence
awk 'BEGIN { print "With this single string I invoke this hex and seal thy fate in a \x27" }'
awk 'BEGIN {print "Careful with hexes one must be... don\x27t want them \x22ing back on thee" }'
# Umm throw some hexes if you prefer
# \x22 = Double quote hex escape sequence
# \x27 = Single hex (quote) sequence
awk -v asq="'" -v adq='"' 'BEGIN { print "Alzheimer infested are encouraged to forget about " asq" and " adq " ing" }'
# Variably I say unto thee, all must stop quoting me!
# -v on cmdline sets a variable
awk '{ print $0 }' list.txt
#Print out all lines in a file called list.txt
awk '{ print "\"" $0 "\""}' test-files/list.txt
awk '{ print "\x22" $0 "\x22" }' test-files/list.txt
#Print out all lines in a file called list.txt surrounded by double quotes
gawk "{ print \"\x22\" $0 \"\x22\" }" test-files\list.txt
gawk "{ print \"\042\" $0 \"\042\" }" test-files\list.txt
# On windows double quoting on each line (dos shell) cygwin works like above
awk '{ print "\x27" $0 "\x27" }' test-files/list.txt
awk '{ print "'\''" $0 "'\''" }' test-files/list.txt
awk '{ print "'"'"'" $0 "'"'"'" }' test-files/list.txt
#Print out all lines in a file surrounded by single quotes
gawk "{ print \"\x27\" $0 \"\x27\" }" test-files\list.txt
gawk "{ print \"\047\" $0 \"\047\" }" test-files\list.txt
# Single quoting each line windows
awk -v SQ="'" '{ cmd="printf \"%d\\n\" \"" SQ $1 "\"" system( cmd ) }'
# This script will convert typed in characters to there ascii values
# Must control-c or control-d to get out
# Kinda buggy cause lags behind by one return/char, so hit enter first then type.
echo A | awk 'BEGIN{for(n=0;n<256;n++)ord[sprintf("%c",n)]=n}{print ord[$1]}'
# Convert ascii char to decimal value
echo 65 | awk '{printf "%c\n", $1}'
echo 66 | awk 'BEGIN{for(n=0;n<256;n++)chr[n]=sprintf("%c",n)}{print chr[$1]}'
# Convert ascii decimal value to char
echo -e "\x41" | awk '{printf "%c\n", $1}'
echo -e "\x53\x70\x65\x6c\x6c\x20\x48\x45\x58\x20\x66\x6f\x72\x20\x6d\x65\x21" | awk '{printf "%s\n", $_}'
# Convert ascii hex to char
awk 'BEGIN {for (i = 1; i < 128; i++) {table = sprintf("%s%c", table, i);}}function charhex (char) {return sprintf("0x%x", index(table, char));} END { print charhex("a"); print charhex(" "); print charhex("'"'"'");}' < /dev/null
# Print out ascii hex values for a " "(space) and '(single quote)
awk 'BEGIN {for (i = 1; i < 128; i++) {table = sprintf("%s%c", table, i);}}function charoct (char) {return sprintf("0x%o", index(table, char));} END { print charoct("a"); print charoct(" "); print charoct("'"'"'");}' < /dev/null
# Print out ascii octal values for a " "(space) and '(single quote)
echo | awk 'BEGIN {for (i = 32; i < 127; i++) {table = sprintf("%s%c%s%x%s%o%s", table, i, "="i"=", i, "=", i, " ")}} END { print table }'
# Print out ascii->decimal->Hex->Octal values (outputs all one line)
awk 'BEGIN {for (i = 32; i < 127; i++) {table = sprintf("%s%c%s%x%s%o%s", table, i, "="i"=", i, "=", i, "\n")}} END { print table }' < /dev/null
# Print out ascii->decimal->Hex->Octal values (with newlines)
awk '{a[NR]=$0} END {print a[NR]; for (i=1;i
default