awk (authors Aho, Weinberher, Kernigan) is a programming language for text processing. A statement consists of a pattern followed by an action. If the pattern is present the action operates on all matching input lines, if not the action operates on all input lines. If the action is absent the default is write to STDOUT. All awk statements should be quoted.
awk variables:
A few elements of awk syntax:
-F#/ /{ }" ";~ !~== > < >= <= !=&& || != += -=$ (followed by a number)FSOFSRSORSNFNRprint(arg)arg to STDOUTlength(string)stringsubstr(string,position)string from position (1-based) to endsubstr(string,position,count)count characters of string starting from position (1-based)gsub(regexp,string)string for all occurrences of rexexp in $0gsub(regexp,string,var)string for all occurrences of rexexp in var& in string argument is a backreference to characters which matched regexp argumenthead -n25 /etc/passwd | awk -F: '{print $3, $1}' # 2 fields, in order
head -n25 /etc/passwd | awk -F: '{OFS = ":"; print $3, $1}' # : field separator
head -n25 /etc/passwd | awk -F: '{print $1, $(NF)}' # first and last fields
head -n25 /etc/passwd | awk -F: '$1 ~ /^....$/ {print $3, $1}' # 4-char name
head -n25 /etc/passwd | awk -F: '$1 ~ /^....$/ {print $3 + 1000, $1}' # UID
head -n25 /etc/passwd | awk -F: '$4 == 100 {print $3 + 1000, $1}' # GID = 100
head -n25 /etc/passwd | awk -F: '$4 == 100 {$3 += 1000} {print $3, $1}' # print all
head -n25 /etc/passwd | awk -F: '$3 < $4 {print $3, $4, $1}' # UID < GID
head -n25 /etc/passwd | awk -F: '$3 < 10 {$4 = $3; print $0}' # new GID
cat /etc/passwd | awk -F: 'length($5) > 16 {print $0}' # long full name
S=abcd1234 # input for following statements
echo $S | awk '{sub("d","X"); print $0}' # substitute
echo $S | awk '{sub(/[a-z]/,"X"); print $0}' # use regular expression
echo $S | awk '{gsub(/[a-z]/,"X"); print $0}' # global
echo $S | awk '{gsub(/[a-z]/,"&X"); print $0}' # & is what matched regex
echo $S | awk '{print substr($0,5)}' # substring beginning with char 5
echo $S | awk '{print substr($0,5,2)}' # beginning with char 5, 2 chars