awk

awk = Aho, Weinberger, and Kernighan

$0 = whole line
$1 = first field
$2 = second field
[...]
$NF = last field
FS = field separator, default FS=\s+
OFS = output field seperator
RS = record (line) separator
ORS = output record seperator
NR = number of records (counter from 1 to n)
NF = total number of fields
FILENAME = name of the current input file
FNR = number of records relative to the current input file
match()
  -> RSTART = where the pattern starts
  -> RLENGTH = the length of the pattern

Starters

Awk – A Tutorial and Introduction – by Bruce Barnett
Awk Introduction Tutorial – 7 Awk Print Examples
10 Awk Tips, Tricks and Pitfalls

BEGIN and END

gawk Manual – Startup and Cleanup Actions
AWK scripting: 10 BEGIN and END block examples

BEGIN { just once }
{ every record by itself }
END { just once }

Built-In Variables

8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

String Functions

gawk Manual – String-Manipulation Functions

String concatenate

awk – concatenate two string variable and assign to a third
gawk Manual – String Concatenation

$ awk '{b=$1$2; print b}' file
$ awk '{b=$1" "$2; print b}' file
$ awk '{b=$1 FS $2; print b}' file

Regex

gawk Manual – Using Dynamic Regexps
gawk Manual – Using Regular Expression Constants
Pass shell variable as a /pattern/ to awk
How to use awk variables in regular expressions?
Passing variable to awk and using that in a regular expression

Shell Variables

Can we use shell variables in awk?

Leave a Reply

Your email address will not be published. Required fields are marked *