awk (gawk) #
Usage #
bash
gawk OPTIONS -f PROGRAM-FILE [--] FILE...
gawk OPTIONS [--] PROGRAM-TEXT FILE...
Options #
text
-F fs
--field-separator fs
The input field separator
-v var=val
--assign var=val
Assign the value val to the variable var
-V
--version
Print version
Variables #
text
FS
The input field separator
RS
The input record separator
OFS
The output field separator
ORS
THE output record separator
OFMT
The output format for numbers, "%.6g", by default.
FIELDWIDTHS
Fixed width for input fields
FPAT
A regular expression for input fields (overrided by FS or FIELDWIDTHS).
IGNORECASE
Controls the case-sensitivity of all regular expression and string operations.
ENVIRON
An array containing the values of the current environment (e.g., ENVIRON["HOME"] might be "/home/arnold").
FNR
The input record number in the current input file.
NF
The number of fields in the current input record.
NR
The total number of input records seen so far.
Control Statements #
text
if (condition) statement [ else statement ]
while (condition) statement
do statement while (condition)
for (expr1; expr2; expr3) statement
for (var in array) statement
break
continue
delete array[index]
delete array
exit [ expression ]
{ statements }
switch (expression) {
case value|regex : statement
...
[ default: statement ]
}
I/O Statements #
text
close(file [, how])
Close file, pipe or co-process.
getline
Set $0 from next input record; set NF, NR, FNR, RT. The getline command returns 1 on success, 0 on end of file, and -1 on an error.
getline <file
Set $0 from next record of file; set NF, RT.
getline var
Set var from next input record; set NR, FNR, RT.
getline var <file
Set var from next record of file, RT.
command | getline [var]
Run command piping the output either into $0 or var, as above, and RT.
command |& getline [var]
Run command as a co-process piping the output either into $0 or var, as above, and RT. (command can also be a socket.)
next
Stop processing the current input record.
nextfile
Stop processing the current input file.
print
Print the current record. The output record is terminated with the value of ORS.
print expr-list
Print expressions. Each expression is separated by the value of OFS. The output record is terminated with the value of ORS.
print expr-list >file
Print expressions on file.
printf fmt, expr-list
Format and print.
printf fmt, expr-list >file
Format and print on file.
system(cmd-line)
Execute the command cmd-line, and return the exit status.
fflush([file])
Flush any buffers associated with the open output file or pipe file.
Additional output redirections:
text
print ... >> file
Appends output to the file.
print ... | command
Writes on a pipe.
print ... |& command
Sends data to a co-process or socket.
Special File Names #
text
-
The standard input.
/dev/stdin
The standard input.
/dev/stdout
The standard output.
/dev/stderr
The standard error output.
/dev/fd/n
The file associated with the open file descriptor n.
Numeric Functions #
text
atan2(y, x)
cos(expr)
exp(expr)
int(expr)
log(expr)
rand()
sin(expr)
sqrt(expr)
srand([expr])
String Functions #
text
asort(s [, d [, how] ])
asorti(s [, d [, how] ])
gsub(r, s [, t])
index(s, t)
length([s])
match(s, r, [, a])
split(s, a [, r [, seps] ])
sprintf(fmt, expr-list)
substr(s, i [, n])
tolower(str)
toupper(str)
Examples #
Output to stderr #
awk
print "You blew it!" > "/dev/stderr"
print "You blew it!" | "cat 1>&2"
Use command line #
awk
json = $1
#json = "{\"text\":\"abc\",\"flag\":\"123\"}"
cmd = "echo '" json "' | jq -r '.text, .flag' | tr '\n' '\t'"
cmd | getline
close(cmd)
print $1 $2
Custom function #
awk
function myfunc(s) {
print NF ":" s
}
{
myfunc($1)
}