Linux/awk

awk
Print all columns from pipe: ... | awk '{print $0}'

Print columns 1 and 2 separated by spaces: ... | awk '{print $1, $2}' ... | awk '{print $1 " " $2}'

Grep for something ... | awk '/somestring/' ... | awk '/somestring/{print $1}'

Sub string: ESX=$( vmware -v | awk '{print $2 substr($3,1,1) substr($3,3,1)}' ) # ESXi50

For loop with if: echo "one two three four five" | awk '{for(i=1;i<10;i++) {if($i!="") printf $i " "}}'

Padding with spaces: awk '{printf "%-30s%s\n", $1, $2}'

Print all remaining fields: ps aux | awk '{ $1=$2=$3=$4=$5=$6=$7=$8=$9=""; print $0 }' | sort -n
 * 1) find longest running processes

powerful awk
"awk is a little programming language, with a syntax close to C in many aspects. It is an interpreted language and the awk interpreter processes the instructions.

Awk is, roughly speaking, a language oriented to manage tables. That is some information which can be grouped inside fields and records. The advantage here is that the record definition (and the field definition) is flexible.

Awk is powerful. It's designed for work with one-line records, but that point could be relaxed"

extract the first column from a file awk '{print $1}' file

renaming files (append .new to "files_list"): ls files_list | awk '{print "mv "$1" "$1".new"}' | sh

Renaming within the name: (although in some cases it will fail, as in file_old_and_old) ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh

remove only files: ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
 * 1) or with awk alone:

remove only directories ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh # or ls -p | grep /$ | wk '{print "rm -r "$1}' ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
 * 1) or with awk alone:

killing processes by name (in this example we kill the process called netscape): kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'` ps auxww | awk '$0~/netscape/&&$0!~/awk/{print $2}' |xargs kill
 * 1) or with awk alone:

Source: UNIX Basics : Examples with awk: A short introduction

Print first two fields in opposite order: awk '{ print $2, $1 }' file

Print lines longer than 72 characters: awk 'length > 72' file

Print length of string in 2nd column awk '{print length($2)}' file

Print hi 28 times yes | head -28 | awk '{ print "hi" }' yes hi | head -n 28

Source: Hartigan/Computer/AWK

References:
 * An Awk Primer

Famous Awk One-Liners
famous Awk one-liners (.txt)


 * Famous Awk One-Liners Explained, Part I - good coders code, great reuse
 * Famous Awk One-Liners Explained, Part II - good coders code, great reuse


 * Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse
 * AWK Cheat-Sheet .pdf
 * AWK Cheat-Sheet .txt

AWK

 * http://www.vectorsite.net/tsawk.html
 * http://www.grymoire.com/Unix/Awk.html
 * http://sparky.rice.edu/~hartigan/awk.html
 * http://www.cs.hmc.edu/qref/awk.html
 * http://robert.wsi.edu.pl/awk/start.html
 * http://www.cs.ucsb.edu/~sherwood/awk/
 * http://www.gnulamp.com/awk.html
 * http://www.ibm.com/developerworks/library/l-awk3.html

String Manipulation
[http://www.askdavetaylor.com/chopping_off_the_last_field_of_each_line.html Chopping off the last field of each line? :: Free Tech Support from Ask Dave Taylor!]: rev inputfile | cut -f2- | rev > outputfile awk '{$NF=""; print $0}'