LinuxCommandLibrary

gawk

GNU implementation of AWK text processor

TLDR

Print specific columns from a file

$ gawk '{print $1, $3}' [path/to/file]
copy
Use a different field separator
$ gawk -F':' '{print $1}' [/etc/passwd]
copy
Sum values in a column
$ gawk '{sum += $1} END {print sum}' [file]
copy
Filter lines matching a pattern
$ gawk '/pattern/ {print}' [file]
copy
Print line numbers with content
$ gawk '{print NR": "$0}' [file]
copy
Run an awk program from a file
$ gawk -f [program.awk] [file]
copy
Use variables from command line
$ gawk -v name="[value]" '{print name, $0}' [file]
copy
Print lines where column 3 > 100
$ gawk '$3 > 100 {print}' [file]
copy

SYNOPSIS

gawk [-F fs] [-v var=val] [-f progfile] ['program'] [file ...]

DESCRIPTION

gawk (GNU awk) is the GNU implementation of the AWK programming language, designed for text processing and data extraction. It processes input line by line, splitting each into fields that can be manipulated and transformed.
An AWK program consists of patterns and actions: pattern { action }. When a line matches a pattern, the associated action executes. Special patterns BEGIN and END run before/after processing any input.
Built-in variables include: $0 (entire line), $1-$n (fields), NR (record/line number), NF (number of fields), FS (field separator), OFS (output field separator), and RS (record separator).
Gawk extends standard AWK with features like: network programming (TCP/UDP connections), time functions, bit manipulation, internationalization, and extension loading. It also supports regular expressions, arrays, user-defined functions, and getline for reading from files/pipes.
Common uses include: log analysis, CSV/TSV processing, report generation, data transformation, and quick text manipulation tasks that would be verbose in other languages.

PARAMETERS

-F fs

Set field separator (default: whitespace).
-f progfile
Read program from file instead of command line.
-v var=value
Assign value to variable before program execution.
-b, --characters-as-bytes
Treat all input data as single-byte characters.
-c, --traditional
Run in POSIX-compatible mode.
-e 'program'
Specify program text (can use multiple -e options).
-i file
Include library file.
-l lib
Load extension library.
-n, --non-decimal-data
Recognize octal and hexadecimal values in input.
-o file
Pretty-print program to file.
-p file
Enable profiling, output to file.
-S, --sandbox
Disable system commands and file access.
--help
Display help information.
--version
Display version information.

CAVEATS

Complex programs may be better suited for Python or Perl. Floating-point precision can be surprising. Different AWK implementations have subtle incompatibilities. Default field splitting may not handle quoted CSV properly (consider csvkit for that).

HISTORY

AWK was created by Alfred Aho, Peter Weinberger, and Brian Kernighan at Bell Labs in 1977 (the name is their initials). gawk (GNU AWK) was developed by the GNU Project starting in 1986, adding significant extensions while maintaining compatibility. It remains the most feature-rich AWK implementation.

SEE ALSO

awk(1), sed(1), perl(1), cut(1), grep(1)

> TERMINAL_GEAR

Curated for the Linux community

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99
BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

$239.99
nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

$129.95
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

$739.99
New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

$54.00
Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99