gawk command in Linux with Examples
Last Updated :
01 Nov, 2024
The gawk command in Linux is a pattern scanning and processing language. No compilation is required, and variables can be used along with numeric functions, string functions, and logical operators.
Gawk is a utility that enables programmers to write highly compact but still effective programs as statements that define text patterns to look for in a text document, and the action to be taken every time such a match is found within a line.
Gawk can be used to:
- Scan a file line by line
- Split each input line into fields
- Compare input line/fields to pattern
- Perform action(s) on matched lines
- Transform data files
- Produce formatted reports
- Format output lines
- Perform arithmetic and string operations
- Execute conditionals and loops
Syntax
gawk [POSIX / GNU style options] -f progfile [--] file ...
gawk [POSIX / GNU style options] [--] 'program' file ...
Basic gawk Command Example
By default, gawk prints every line of data from the named file:
gawk '{ print }' mobile.txt
Terminal output of the gawk command printing all lines from the mobile.txtBasic Options and Usage
Option | Description |
---|
-f progfile, --file=progfile | Read the AWK program source from the file program-file, instead of from the first command line argument |
---|
-F fs, --field-separator=fs | Use FS for the input field separator (the value of the FS predefined variable) |
---|
-v var=val, --assign=var=val | Assign the value val to the variable var, before execution of the program begins |
---|
Examples of Option Usage
Specifying field separator using -F option:
gawk -F: '{ print $1 }' /etc/passwd
Terminal output of gawk command with -F optionUsing -f option to read program from a file:
gawk -F: -f mobile.txt /etc/passwd
Built-In Variables
- NR: Keeps a running count of the number of input lines
- NF: Keeps a count of the number of fields in the current input record
- FS: Holds the field separator character
- RS: It saves the record separator character at the present time
- OFS: It saves the output field separator
- ORS: It saves the output record separator
Example using NR:
gawk '{ print NR "-" $1 }' mobile.txt
Terminal output of gawk command using NR variableAdvanced Usage Examples
Assume that we have a dataset in a file named mobile.txt:
Name Mobile Email
John 1234567890 [email protected]
Sunil 9876543210 [email protected]
Alice 5555555555 [email protected]
1. Print lines matching a pattern:
gawk '/Sunil/ ' mobile.txt
output2. Print specific fields:
gawk '{ print $2 }' mobile.txt
output3. Count the number of lines in a file:
gawk 'END NR}' mobile.txt
outputConclusion
The gawk command is actually one of the most powerful tools in Linux when it comes to text processing. The tool can scan files, process patterns, and execute many actions; it is very valuable to both system administrators and programmers. Mastering gawk will allow users to easily manipulate text data, generate reports, or automatically automate many other text-processing activities.
Similar Reads
fg command in Linux with examples The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it.Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently runn
3 min read
fgrep command in Linux with examples The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
4 min read
file command in Linux with examples The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
3 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
Finger command in Linux with Examples The 'finger' command is a powerful utility in Linux used to display information about users logged into the system. This command is commonly used by system administrators to retrieve detailed user information, including login name, full name, idle time, login time, and sometimes the user's email add
4 min read
fmt command in Linux with examples fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specif
4 min read
fold command in Linux with examples The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command. Syntax of `
3 min read
for command in Linux with Examples IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain.Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of e
2 min read
free Command in Linux with examples While using LINUX there might come a situation when you are willing to install a new application (big in size) and you wish to know the amount of free memory available on your system. In LINUX, there exists a command line utility for this and that is the 'free' command which displays the total amoun
6 min read
Fun Commands in Linux Linux isn't just for coding and administrationâit can also be a lot of fun. With a variety of terminal commands, you can add some entertainment to your Linux experience. Below is a list of some cool and fun commands you can use in Linux to enhance your terminal experience. Weâll also cover how to in
3 min read