Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions.
Awk is abbreviated from the names of the developers - Aho, Weinberger, and Kernighan.
Syntax:awk options 'selection _criteria {action }' input-file > output-fileOption Description
-F
Sets a custom field separator -f
Reads awk program from a file '{}'
Encloses action to take on match WHAT CAN WE DO WITH AWK?
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
2. Useful For:
(a) Transform data files
(b) Produce formatted reports
3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Sample Commands
Example:
Consider the following text file as the input file for all cases below:
$cat > employee.txt
ajay manager account 450001. Print All Lines (Default Behavior)
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
By default Awk prints every line of data from the specified file.
$ awk '{print}' employee.txt
Output:
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure.
2. Search Lines with a Keyword$ awk '/manager/ {print}' employee.txt
Output:
ajay manager account 45000
varun manager sales 50000
amit manager account 47000
In the above example, the awk command prints all the line which matches with the ‘manager’.
3. Print Specific ColumnsFor each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.
$ awk '{print $1,$4}' employee.txt
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example, $1 and $4 represents Name and Salary fields respectively.
Built-In Variables In AwkAwk's built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields.
Examples:
Use of NR built-in variables (Display Line Number)$ awk '{print NR,$0}' employee.txt
Output:
1 ajay manager account 45000
2 sunil clerk account 25000
3 varun manager sales 50000
4 amit manager account 47000
5 tarun peon sales 15000
6 deepak clerk sales 23000
7 sunil peon sales 13000
8 satvik director purchase 80000
In the above example, the awk command with NR prints all the lines along with the line number.
Use of NF built-in variables (Display Last Field)$ awk '{print $1,$NF}' employee.txt
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example $1 represents Name and $NF represents Salary. We can get the Salary using $NF , where $NF represents last field.
Another use of NR built-in variables (Display Line From 3 to 6)$ awk 'NR==3, NR==6 {print NR,$0}' employee.txt
Output:
3 varun manager sales 50000More Examples
4 amit manager account 47000
5 tarun peon sales 15000
6 deepak clerk sales 23000
For the given text file:
$cat > geeksforgeeks.txt1) To print the first item along with the row number(NR) separated with ” - “ from each line in geeksforgeeks.txt:A B C
Tarun A12 1
Man B6 2
Praveen M42 3
$ awk '{print NR "- " $1 }' geeksforgeeks.txt
1 - A2) To return the second column/item from geeksforgeeks.txt:
2 - Tarun
3 – Manav
4 - Praveen
The question should be:- To return the second column/item from geeksforgeeks.txt:
$ awk '{print $2}' geeksforgeeks.txt
B3) To print any non empty line if present
A12
B6
M42
$ awk 'NF < 0' geeksforgeeks.txt
here NF should be 0 not less than and the user have to print the line number also:
correct answer : awk 'NF == 0 {print NR}' geeksforgeeks.txt
OR
awk 'NF <= 0 {print NR}' geeksforgeeks.txt
04) To find the length of the longest line present in the file:
$ awk '{ if (length($0) > max) max = length($0) } END { print max }' geeksforgeeks.txt
135) To count the lines in a file:
$ awk 'END { print NR }' geeksforgeeks.txt
36) Printing lines with more than 10 characters:
$ awk 'length($0) > 10' geeksforgeeks.txt
Tarun A12 17) To find/check for any string in any specific column:
Praveen M42 3
$ awk '{ if($3 == "B6") print $0;}' geeksforgeeks.txt8) To print the squares of first numbers from 1 to n say 6:
$ awk 'BEGIN { for(i=1;i<=6;i++) print "square of", i, "is",i*i; }'
square of 1 is 1Conclusion
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25
square of 6 is 36
The Unix/Linux AWK command is a very straightforward yet extremely useful utility for any text file, log, or command-line data you're dealing with. A beginner or an old hand system admin, the AWK makes your life easier by assisting you in searching, filtering, and formatting data instantly and efficiently — all from the terminal.
With AWK, you don't have to program lengthy scripts. A one-liner can yield employee salaries, remove logs, or even spit out quick reports. It is pattern-aware, breaks lines into fields, and allows you to do things such as print, count, compute, and format — all within a few lines.
From loops, NR, FS, and $1 to printing a particular row, column, or even automating tiny things — AWK saves time, prevents manual errors, and increases productivity on Linux platforms.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4