awk Command in Linux: Syntax, Patterns, and Examples

Awk is a text-processing language for extracting columns, filtering records, and reshaping structured data on the command line. It is built into every Linux and Unix system and handles tasks ranging from one-line column extraction to multi-stage data analysis.
Unlike most procedural programming languages, awk is data-driven. You define a set of rules consisting of patterns and actions, and awk applies them to each line of input automatically. This guide covers awk syntax, patterns, actions, built-in variables, string functions, arrays, and practical examples.
What Is awk?
Awk is a programming language and command-line tool for processing text, named after its creators Aho, Weinberger, and Kernighan. It reads input one record at a time, splits each record into fields, and runs pattern-action rules against them. Most Linux distributions ship with gawk (GNU awk), mawk, or nawk, all of which implement the same POSIX core.
Common uses include extracting specific columns from command output, summing or counting values across rows, filtering log lines that match a pattern, and reformatting structured data such as CSV or TSV. For plain column extraction on a fixed delimiter, cut
is usually enough. Awk becomes the better choice once you need conditions, arithmetic, or control over the output format.
How awk Works
There are several different implementations of awk. We will use the GNU implementation of awk, which is called gawk. On many Linux systems, awk points to gawk, while on others it may point to a different implementation such as mawk.
Records and Fields
Awk processes textual data files and streams. The input data is divided into records and fields. Awk operates on one record at a time until the end of the input is reached. Records are separated by a character called the record separator. The default record separator is the newline character, meaning each line in the text data is a record. A new record separator can be set using the RS variable.
Records consist of fields that are separated by the field separator. By default, fields are separated by whitespace, including one or more tab, space, and newline characters.
The fields in each record are referenced by the dollar sign ($) followed by the field number beginning with 1. The first field is represented with $1, the second with $2, and so on. The last field can also be referenced with the special variable $NF. The entire record can be referenced with $0.
Here is a visual representation showing how to reference records and fields:
tmpfs 788M 1.8M 786M 1% /run/lock
/dev/sda1 234G 191G 31G 87% /
|-------| |--| |--| |--| |-| |--------|
$1 $2 $3 $4 $5 $6 ($NF) --> fields
|-----------------------------------------|
$0 --> recordAwk Program
To process text with awk, you write a program that tells the command what to do. The program consists of a series of rules and optional user-defined functions. Each rule contains a pattern and action pair. Rules are separated by newlines or semicolons (;). A typical awk program looks like this:
pattern { action }
pattern { action }
...When awk processes data, if the pattern matches the record, it performs the specified action on that record. When a rule has no pattern, every input record is matched. When a rule has no action, it defaults to printing the entire record.
An awk action is enclosed in braces ({}) and consists of statements. Each statement specifies the operation to be performed. Multiple statements are separated by newlines or semicolons (;).
When writing awk programs, everything after the hash mark (#) and until the end of the line is considered a comment. Long lines can be broken into multiple lines using the continuation character, backslash (\).
Executing Awk Programs
An awk program can be run in several ways. If the program is short and simple, it can be passed directly to the awk interpreter on the command line:
awk 'program' input-file...When running the program on the command line, it should be enclosed in single quotes ('') so the shell does not interpret the program.
If the program is large and complex, it is best to put it in a file and use the -f option to pass the file to the awk command:
awk -f program-file input-file...In the examples below, we will use a file named “teams.txt” that looks like the one below:
Bucks Milwaukee 60 22 0.732
Raptors Toronto 58 24 0.707
76ers Philadelphia 51 31 0.622
Celtics Boston 49 33 0.598
Pacers Indiana 48 34 0.585Awk Syntax
The general syntax of the awk command is:
awk [OPTIONS] 'pattern { action }' fileThe most commonly used options are:
-F- Sets the input field separator (same as settingFSinside the program).-v var=value- Assigns a value to a variable before the program begins executing.-f program-file- Reads the awk program from a file instead of the command line.
Awk can also read input from standard input (stdin) when no file is specified. This makes it useful in pipelines:
command | awk '{ print $1 }'Awk Patterns
Patterns control whether the associated action is executed. Awk supports several types of patterns, including regular expressions, relational expressions, ranges, and special patterns.
When the rule has no pattern, each input record is matched. Here is an example of a rule containing only an action:
awk '{ print $3 }' teams.txtThe program prints the third field of each record:
60
58
51
49
48Regular Expression Patterns
A regular expression or regex is a pattern that matches a set of strings. Awk regular expression patterns are enclosed in slashes (//):
/regex pattern/ { action }The most basic example is a literal character or string matching. To display the first field of each record that contains “0.5”, run the following command:
awk '/0.5/ { print $1 }' teams.txtCeltics
PacersThe pattern can be any type of extended regular expression . Here is an example that prints the first field if the record starts with two or more digits:
awk '/^[0-9][0-9]/ { print $1 }' teams.txt76ersRelational Expression Patterns
Relational expression patterns are generally used to match the content of a specific field or variable.
By default, regular expression patterns are matched against the entire record. To match a regex against a field, specify the field and use the “contain” comparison operator (~) against the pattern.
For example, to print the first field of each record whose second field contains “ia”:
awk '$2 ~ /ia/ { print $1 }' teams.txt76ers
PacersTo match fields that do not contain a given pattern, use the !~ operator:
awk '$2 !~ /ia/ { print $1 }' teams.txtBucks
Raptors
CelticsYou can compare strings or numbers for relationships such as greater than, less than, or equal. The following command prints the first field of all records whose third field is greater than 50:
awk '$3 > 50 { print $1 }' teams.txtBucks
Raptors
76ersRange Patterns
Range patterns consist of two patterns separated by a comma:
pattern1, pattern2All records starting with a record that matches the first pattern until a record that matches the second pattern are matched.
Here is an example that prints the first field of all records starting from the record including “Raptors” until the record including “Celtics”:
awk '/Raptors/,/Celtics/ { print $1 }' teams.txtRaptors
76ers
CelticsThe patterns can also be relational expressions. The command below prints all records starting from the one whose fourth field is equal to 31 until the one whose fourth field is equal to 33:
awk '$4 == 31, $4 == 33 { print $0 }' teams.txt76ers Philadelphia 51 31 0.622
Celtics Boston 49 33 0.598Range patterns cannot be combined with other pattern expressions.
Special Expression Patterns
Awk includes the following special patterns:
BEGIN- Used to perform actions before records are processed.END- Used to perform actions after records are processed.
The BEGIN pattern is commonly used to set variables, and the END pattern to process data from the records such as calculations.
The following example prints “Start Processing.”, then prints the third field of each record, and finally “End Processing.”:
awk 'BEGIN { print "Start Processing." }; { print $3 }; END { print "End Processing." }' teams.txtStart Processing.
60
58
51
49
48
End Processing.If a program has only a BEGIN pattern, actions are executed, and the input is not processed. If a program has only an END pattern, the input is processed before performing the rule actions.
The GNU version of awk also includes two more special patterns, BEGINFILE and ENDFILE, which allow you to perform actions when processing files.
Combining Patterns
Awk allows you to combine two or more patterns using the logical AND operator (&&) and logical OR operator (||).
Here is an example that uses the && operator to print the first field of those records whose third field is greater than 50 and the fourth field is less than 30:
awk '$3 > 50 && $4 < 30 { print $1 }' teams.txtBucks
RaptorsAwk Actions
Awk actions are enclosed in braces ({}) and executed when the pattern matches. An action can have zero or more statements. Multiple statements are executed in the order they appear and must be separated by newlines or semicolons (;).
Several types of action statements are supported in awk:
- Expressions, such as variable assignment, arithmetic operators, increment, and decrement operators.
- Control statements, used to control the flow of the program (
if,for,while,switch, and more). - Output statements, such as
printandprintf. - Compound statements, to group other statements.
- Input statements, to control the processing of the input.
- Deletion statements, to remove array elements.
The print Statement
The print statement is the most commonly used awk statement. It prints text, records, fields, and variables.
When printing multiple items, you need to separate them with commas. Here is an example:
awk '{ print $1, $3, $5 }' teams.txtThe printed items are separated by single spaces:
Bucks 60 0.732
Raptors 58 0.707
76ers 51 0.622
Celtics 49 0.598
Pacers 48 0.585If you do not use commas, there will be no space between the items:
awk '{ print $1 $3 $5 }' teams.txtThe printed items are concatenated:
Bucks600.732
Raptors580.707
76ers510.622
Celtics490.598
Pacers480.585When print is used without an argument, it defaults to print $0, printing the current record.
To print custom text, you must quote the text with double-quote characters:
awk '{ print "The first field:", $1}' teams.txtThe first field: Bucks
The first field: Raptors
The first field: 76ers
The first field: Celtics
The first field: PacersYou can also print special characters such as newline:
awk 'BEGIN { print "First line\nSecond line\nThird line" }'First line
Second line
Third lineThe printf Statement
The printf statement gives you more control over the output format. Unlike print, printf does not automatically add a newline after each output. Here is an example that inserts line numbers:
awk '{ printf "%3d. %s\n", NR, $0 }' teams.txt 1. Bucks Milwaukee 60 22 0.732
2. Raptors Toronto 58 24 0.707
3. 76ers Philadelphia 51 31 0.622
4. Celtics Boston 49 33 0.598
5. Pacers Indiana 48 34 0.585Common printf format specifiers include %s for strings, %d for integers, %f for floating-point numbers, and %x for hexadecimal.
Redirecting Output
Both print and printf can write somewhere other than standard output. Add > and a file name to the end of the statement to send the result to a file. The following program splits the team names into two files based on the third field:
> operator truncates existing files. Run this example only if strong.txt and average.txt do not contain data you need to keep.awk '$3 > 50 { print $1 > "strong.txt" } $3 <= 50 { print $1 > "average.txt" }' teams.txtNothing appears in the terminal, because every print was redirected. Reading the first file back shows the three teams that matched:
cat strong.txtBucks
Raptors
76ersAwk truncates the file the first time it opens it, and every later write in the same run is appended. Use >> instead when you want to keep the previous contents of a file.
You can also pipe output to a shell command with |. Here each team name is sent to sort:
awk '{ print $1 | "sort" } END { close("sort") }' teams.txt76ers
Bucks
Celtics
Pacers
RaptorsThe close() call in the END block closes the pipe and waits for sort to finish. Without it, awk keeps the pipe open until the program exits, and the sorted names can show up after everything else your program printed.
Control Statements
Awk supports standard control flow statements. The if/else statement lets you execute different actions based on conditions:
awk '{ if ($3 > 50) print $1, "- strong"; else print $1, "- average" }' teams.txtBucks - strong
Raptors - strong
76ers - strong
Celtics - average
Pacers - averageThe for loop is useful for iterating over a range of values:
awk 'BEGIN { for (i = 1; i <= 5; i++) print "Square of", i, "is", i*i }'Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25The while loop works similarly. Here is the same example using while:
awk 'BEGIN { i = 1; while (i <= 5) { print "Square of", i, "is", i*i; i++ } }'The next and exit Statements
The next statement stops processing the current record and moves straight on to the following one. It is the usual way to skip records that the remaining rules should not touch. Here we skip the record that contains “76ers”:
awk '/76ers/ { next } { print $1 }' teams.txtBucks
Raptors
Celtics
PacersThe second rule never runs for the skipped record, so “76ers” is missing from the output.
The exit statement stops reading input altogether and jumps to the END block, if the program has one. Use it when you have found what you were looking for and there is no reason to read the rest of the file:
awk 'NR == 3 { print $1; exit }' teams.txt76ersAwk prints the first field of the third record and quits without reading the last two lines.
User-Defined Functions
When the same calculation shows up in more than one rule, you can move it into a function. A definition starts with the function keyword, followed by the name, the parameter list, and a body enclosed in braces:
function name(parameter, parameter) {
statements
return value
}Functions can be defined anywhere in the program, including below the rules that call them. The program below defines a ratio() function and uses it to recalculate the win percentage from the third and fourth fields:
awk '
function ratio(won, lost) {
return won / (won + lost)
}
{ printf "%s %.3f\n", $1, ratio($3, $4) }
' teams.txtBucks 0.732
Raptors 0.707
76ers 0.622
Celtics 0.598
Pacers 0.585The numbers match the fifth field of the file, which tells us the function is doing the right thing.
Awk has no keyword for declaring local variables. Everything is global unless it appears in the parameter list, so the convention is to add extra parameters that the caller never passes and treat them as locals. Awk sets them to empty on every call:
awk 'function join(a, b, sep) { sep = "-"; return a sep b } BEGIN { print join("x", "y") }'x-yThe wide gap before sep carries no meaning for awk, but it is a widely used convention that marks the parameter as a local variable rather than a real argument.
Accumulator Variables
Variables in awk do not have to be declared and start out empty, which makes them convenient for building a running total as the records go by. The following command calculates the sum of the values stored in the third field across all lines:
awk '{ sum += $3 } END { printf "%d\n", sum }' teams.txt266Running Programs from Files
When writing longer programs, you should create a separate program file:
BEGIN {
for (i = 1; i <= 5; i++) {
print "Square of", i, "is", i*i
}
}Run the program by passing the file name to the awk interpreter:
awk -f prg.awkYou can also run an awk program as an executable by using the shebang
directive and setting the awk interpreter:
#!/usr/bin/awk -f
BEGIN {
for (i = 1; i <= 5; i++) {
print "Square of", i, "is", i*i
}
}Save the file and make it executable :
chmod +x prg.awkYou can now run the program by entering:
./prg.awkBuilt-in Variables
Awk has a number of built-in variables that contain useful information and allow you to control how the program is processed. Below are the most common built-in variables:
NF- The number of fields in the current record.NR- The number of the current record (line number across all files).FNR- The number of the current record in the current file. UnlikeNR,FNRresets to 1 for each new file.FILENAME- The name of the input file currently being processed.FS- The input field separator (default: whitespace).RS- The input record separator (default: newline).OFS- The output field separator (default: space).ORS- The output record separator (default: newline).OFMT- The output format for numbers (default:"%.6g").
Here is an example showing how to print the file name and the number of lines (records):
awk 'END { print "File", FILENAME, "contains", NR, "lines." }' teams.txtFile teams.txt contains 5 lines.The OFS variable controls what character is placed between fields when you use a comma in print. In the following example, we set it to a tab:
awk 'BEGIN { OFS = "\t" } { print $1, $3, $5 }' teams.txtBucks 60 0.732
Raptors 58 0.707
76ers 51 0.622
Celtics 49 0.598
Pacers 48 0.585NR counts records across every input file, while FNR starts again at 1 for each file. The difference only becomes visible when you pass more than one file. For the next example, we will add a second file named “teams2.txt”:
Heat Miami 44 38 0.537
Nets Brooklyn 42 40 0.512Now print both counters together with the file name:
awk '{ print FILENAME, FNR, NR, $1 }' teams.txt teams2.txtteams.txt 1 1 Bucks
teams.txt 2 2 Raptors
teams.txt 3 3 76ers
teams.txt 4 4 Celtics
teams.txt 5 5 Pacers
teams2.txt 1 6 Heat
teams2.txt 2 7 NetsNR keeps climbing to 7, while FNR drops back to 1 as soon as awk opens the second file. This is what you use when a program has to treat the first file differently from the rest.
Variables in awk can be set at any line in the program. To define a variable for the entire program, put it in a BEGIN pattern.
Changing the Field and Record Separator
The default value of the field separator is any number of space or tab characters. It can be changed by setting the FS variable.
For example, to set the field separator to .:
awk 'BEGIN { FS = "." } { print $1 }' teams.txtBucks Milwaukee 60 22 0
Raptors Toronto 58 24 0
76ers Philadelphia 51 31 0
Celtics Boston 49 33 0
Pacers Indiana 48 34 0For multi-character separators, FS is interpreted as a regular expression. Escape regex characters when you need a literal separator. For example, to split records on two literal dots, use [.][.]:
printf 'alpha..beta..gamma\n' | awk 'BEGIN { FS = "[.][.]" } { print $1 }'alphaWhen running awk one-liners on the command line, you can use the -F option to change the field separator:
awk -F "." '{ print $1 }' teams.txtBy default, the record separator is a newline character and can be changed using the RS variable.
Here is an example showing how to change the record separator to .:
awk 'BEGIN { RS = "." } { print $1 }' teams.txtBucks
732
707
622
598
585Each dot now ends a record, so the first record stops right before “0.732” and the next one begins with “732”. Because the newline is no longer a record separator, it is treated as ordinary whitespace between fields, which is why the team names after the first one never appear as $1.
String Functions
Awk includes a rich set of built-in string functions for text manipulation.
length()
The length() function returns the number of characters in a string. When called without an argument, it returns the length of the current record:
awk '{ print $1, length($1) }' teams.txtBucks 5
Raptors 7
76ers 5
Celtics 7
Pacers 6substr()
The substr(string, start, length) function extracts a substring. The start position is 1-based. If length is omitted, it returns everything from start to the end of the string:
awk '{ print substr($1, 1, 3) }' teams.txtBuc
Rap
76e
Cel
Pacsplit()
The split(string, array, separator) function splits a string into an array and returns the number of elements. In the following example, we split a colon-separated string:
echo "one:two:three" | awk '{ n = split($0, a, ":"); for (i = 1; i <= n; i++) print a[i] }'one
two
threesub() and gsub()
The sub(regex, replacement, target) function replaces the first match of a regex in the target string. The gsub() function replaces all matches:
echo "hello world hello" | awk '{ gsub(/hello/, "hi"); print }'hi world hiIf the target is omitted, $0 (the entire record) is used. For a plain search and replace across a whole file, sed
is usually the shorter tool. Reach for gsub() when the replacement depends on a field, a condition, or a calculation.
tolower() and toupper()
These functions convert strings to lowercase or uppercase:
awk '{ print toupper($1) }' teams.txtBUCKS
RAPTORS
76ERS
CELTICS
PACERSindex() and match()
The index(string, target) function returns the position of the first occurrence of target in string, or 0 if not found. The match(string, regex) function does the same but with a regular expression:
awk '{ print $1, index($1, "er") }' teams.txtBucks 0
Raptors 0
76ers 3
Celtics 0
Pacers 4Arrays
Awk supports associative arrays, which use strings as indices (keys). Arrays do not need to be declared before use.
Creating and Accessing Arrays
You can assign values to array elements using array[key] = value:
awk 'BEGIN { fruits["apple"] = 5; fruits["banana"] = 3; print fruits["apple"], fruits["banana"] }'5 3Iterating Over Arrays
Use the for (key in array) syntax to iterate over all elements in an array:
awk '{ teams[$1] = $3 } END { for (name in teams) print name, teams[name] }' teams.txtThis stores each team name and their wins count, then prints them all at the end. Note that the order of elements in awk associative arrays is not guaranteed.
Deleting Array Elements
Use the delete statement to remove an element from an array:
awk 'BEGIN { a["x"] = 1; a["y"] = 2; delete a["x"]; for (k in a) print k, a[k] }'y 2Counting Occurrences
Associative arrays are commonly used for counting. The technique is always the same: use the value you want to count as the array key, then increment that element. The teams file has no repeated values, so the examples below use a small log file named “events.txt”:
error disk full
warning low memory
error disk full
info backup done
error network timeoutTo count how many times each severity level appears, use the first field as the key:
awk '{ counts[$1]++ } END { for (level in counts) print counts[level], level }' events.txt | sort -rn3 error
1 warning
1 infoAwk creates one array element per distinct value in the first field and increments it on every match. The END block prints the totals once the whole file has been read. Since awk does not guarantee the order of array elements, the result is piped through sort
to put the most frequent level first.
Looping over the fields of each record counts every word instead of just the first one:
awk '{ for (i = 1; i <= NF; i++) words[$i]++ } END { for (w in words) print words[w], w }' events.txt | sort -rn3 error
2 full
2 disk
1 warning
1 timeout
1 network
1 memory
1 low
1 info
1 done
1 backupThe NF variable holds the number of fields in the current record, so the loop visits every word on the line no matter how long it is.
Practical Examples
This section demonstrates common real-world uses of awk.
Processing /etc/passwd
The /etc/passwd file uses colons as field separators. To list all usernames and their shells:
awk -F: '{ print $1, $7 }' /etc/passwdTo find users with /bin/bash as their shell:
awk -F: '$7 == "/bin/bash" { print $1 }' /etc/passwdThis is a more selective version of the usual way to list users on a Linux system , since it filters on the login shell instead of printing every account.
Parsing Log Files
To extract IP addresses from an Apache or Nginx access log (where the IP is the first field):
awk '{ print $1 }' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10This pipeline extracts the IPs, counts occurrences with uniq
, sorts by frequency, and shows the top 10.
Piped Commands
Awk is frequently used in pipelines to extract specific columns from command output. To show the name and CPU usage of running processes:
ps aux | awk '{ print $11, $3 }' | head -10To display filesystem usage with only the mount point and used percentage:
df -h | awk 'NR > 1 { print $6, $5 }'The NR > 1 pattern skips the header line.
Summing a Column
To calculate the total size of all files listed by ls -l:
ls -l | awk 'NR > 1 { sum += $5 } END { print "Total bytes:", sum }'Counting Records
To count the number of lines that match a pattern:
awk '/error/ { count++ } END { print count }' /var/log/syslogRemoving Duplicate Lines
To remove duplicate lines while preserving order:
awk '!seen[$0]++' file.txtThis works by using the current line as an array key. The first time a line is seen, seen[$0] is 0 (false), so !seen[$0] is true and the line is printed. On subsequent occurrences, the value is non-zero, so the line is skipped.
CSV Processing
To process a simple CSV file without quoted fields, set the field separator to a comma:
awk -F, '{ print $1, $3 }' data.csvGawk 5.3 and later include a CSV parser that handles quoted commas, doubled quotes, and embedded newlines. Enable it with the --csv option:
gawk --csv '{ print $1, $3 }' data.csvFor gawk 4.0 through 5.2, the FPAT variable can parse quoted fields that stay on a single line:
gawk 'BEGIN { FPAT = "([^,]*)|(\"([^\"]|\"\")+\")" } { print $1, $3 }' data.csvThe FPAT method keeps the enclosing quotes and does not support embedded newlines. Use --csv or a dedicated CSV tool when you need full CSV parsing.
Using Shell Variables in Awk Programs
If you are using the awk command in shell scripts, you will often need to pass a shell variable to the awk program. One option is to enclose the program with double instead of single quotes and substitute the variable in the program. However, this approach makes your awk program more complex as you must escape the awk variables.
The recommended way to use shell variables in awk programs is to assign the shell variable to an awk variable using the -v option. Here is an example:
num=51
awk -v n="$num" 'BEGIN {print n}'51You can also pass multiple variables:
min=50
max=60
awk -v low="$min" -v high="$max" '$3 >= low && $3 <= high { print $1, $3 }' teams.txtBucks 60
Raptors 58
76ers 51Quick Reference
For a printable quick reference, see the Awk cheatsheet .
| Task | Command |
|---|---|
| Print a specific field | awk '{ print $2 }' file |
| Print multiple fields | awk '{ print $1, $3 }' file |
| Filter by pattern | awk '/pattern/ { print }' file |
| Filter by field value | awk '$3 > 50 { print }' file |
| Set field separator | awk -F: '{ print $1 }' file |
| Use BEGIN/END | awk 'BEGIN { print "Header" } { print } END { print "Footer" }' file |
| Sum a column | awk '{ sum += $3 } END { print sum }' file |
| Count matching lines | awk '/pattern/ { c++ } END { print c }' file |
| Remove duplicates | awk '!seen[$0]++' file |
| Print line numbers | awk '{ print NR, $0 }' file |
| Replace text | awk '{ gsub(/old/, "new"); print }' file |
| Pass shell variable | awk -v x="$var" '{ print x, $1 }' file |
| Run program from file | awk -f script.awk file |
| Print last field | awk '{ print $NF }' file |
| Skip a record | awk '/skip/ { next } { print }' file |
| Stop after a match | awk '/found/ { print; exit }' file |
| Write to a file | awk '{ print $1 > "out.txt" }' file |
| Count per key | awk '{ c[$1]++ } END { for (k in c) print c[k], k }' file |
| Define a function | awk 'function f(x) { return x * 2 } { print f($1) }' file |
FAQ
What is the difference between awk and sed?
Awk is a full programming language designed for field-based text processing. It excels at working with structured, columnar data. Sed is a stream editor optimized for line-by-line text transformations such as find-and-replace. Use awk when you need to extract or compute values from specific columns, and sed when you need simple text substitutions.
How do I print a specific column in awk?
Use the dollar sign followed by the column number. For example, awk '{ print $2 }' file prints the second column. You can print multiple columns by separating them with commas: awk '{ print $1, $3 }' file.
What is the difference between awk and gawk?
Gawk (GNU awk) is the GNU implementation of the awk language. It is fully compatible with the POSIX awk specification and adds extensions such as BEGINFILE/ENDFILE patterns, the FPAT variable for field-based parsing, and network I/O. Depending on the distribution, awk may point to gawk or to another implementation.
How do I define a function in awk?
Use the function keyword followed by the name, a parameter list, and a body in braces, for example function double(n) { return n * 2 }. Definitions can appear anywhere in the program, including after the rules that call them. Awk has no local variable keyword, so declare extra parameters that the caller never passes and use those as locals.
How do I use awk with CSV files?
For simple CSV files without quoted fields, set the field separator to a comma with -F,. With gawk 5.3 or later, use gawk --csv for quoted commas, doubled quotes, and embedded newlines. Older gawk releases can use FPAT for quoted fields that remain on one line. For more complex CSV processing, consider a dedicated tool such as Miller.
Conclusion
Awk is a practical tool for column extraction, filtering, reporting, and text analysis from the Linux command line. For deeper language details and GNU-specific features, see the official Gawk manual .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page