The Wayback Machine - https://web.archive.org/web/20250116090625/https://www.geeksforgeeks.org/gawk-command-in-linux-with-examples/
Open In App

gawk command in Linux with Examples

Last Updated : 01 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
2024-10-24_18-36

Terminal output of the gawk command printing all lines from the mobile.txt

Basic 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
2024-10-24_18-39

Terminal output of gawk command with -F option

Using -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
2024-10-24_18-42

Terminal output of gawk command using NR variable

Advanced Usage Examples

Assume that we have a dataset in a file named mobile.txt:

Name    Mobile      Email
John 1234567890 john@example.com
Sunil 9876543210 sunil@example.com
Alice 5555555555 alice@example.com

1. Print lines matching a pattern:

gawk '/Sunil/ ' mobile.txt
2024-10-24_18-44

output

2. Print specific fields:

gawk '{ print $2 }' mobile.txt
2024-10-24_18-45

output

3. Count the number of lines in a file:

gawk 'END  NR}' mobile.txt
2024-10-24_18-46

output

Conclusion

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.

gawk command in Linux with Examples – FAQs

What is the difference between awk and gawk?

gawk is the GNU implementation of awk with additional features. On most Linux systems, awk is symlinked to gawk.

Does gawk process more than one file at a time?

Yes, gawk can process multiple files. Simply list the files as arguments: gawk ‘ $1}’ file1.txt file2.txt

How do I use gawk to calculate something with numeric data?

gawk can perform arithmetic operations. For example, to sum values: gawk ‘0 END sum}’ data.txt

Is it possible to use gawk for regular expressions?

Yes, gawk supports regular expressions. For example: gawk ‘/pattern/’ file.txt will print lines matching the pattern.

Can gawk be used in a shell script?

Yes, gawk can be used in shell scripts. You can embed gawk commands or call separate gawk script files within your shell scripts.


Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

three90RightbarBannerImg