Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

awk Command in unix/linux with Examples

Last Updated on December 11, 2023 by Abhishek Sharma


The awk command in Unix/Linux is a versatile and powerful text-processing tool used for manipulating and processing text or data files. Named after its creators—Aho, Weinberger, and Kernighan—awk is primarily used for handling data either from files, standard input, or through shell pipelines. It excels at extracting and transforming data, allowing users to perform various operations like searching for patterns, processing fields, performing calculations, and generating reports.

awk operates on a per-line basis, executing actions or commands based on patterns defined within the program. Its concise syntax and built-in functionalities make it an invaluable tool for data extraction, formatting, and reporting within the Unix/Linux command-line environment.

What is awk Command in unix/linux with Examples?

The awk command in Unix/Linux is a powerful and versatile text-processing tool used for manipulating and processing text or data files. Named after its creators—Alfred Aho, Peter Weinberger, and Brian Kernighan—awk is designed to operate on data either from files, standard input, or through shell pipelines. It is especially useful for performing data extraction, manipulation, and reporting tasks.

Example of awk Command in Unix/Linux

Here are some examples illustrating the usage of awk:

1. Print Specific Columns from a File:

awk '{print $1, $3}' file.txt

This command prints the first and third columns from file.txt.

2. Perform Arithmetic Operations:

echo "5 10" | awk '{print $1 + $2}’

Using a pipe (|), this command adds the numbers 5 and 10 and prints the result.

3. Search and Print Lines Matching a Pattern:

awk '/pattern/ {print}' file.txt

This command searches for the "pattern" in file.txt and prints lines that match the pattern.

4. Set Custom Field Separator:

awk -F':' '{print $1}' /etc/passwd

Using -F, this command uses : as the field separator to extract the first field (username) from the /etc/passwd file.

5. Filter Data Based on Condition:

awk '$3 > 100 {print $1}' file.txt

This command prints the first column from file.txt if the value in the third column is greater than 100.

6. Calculate Average from Numbers in a File:

awk '{sum+=$1} END {print "Average =", sum/NR}' numbers.txt

Here, awk calculates the average of numbers in numbers.txt and prints the result in the END block using NR (number of records).

awk operates on a per-line basis, where it scans input text or data, matches patterns, and performs actions based on defined patterns and commands. It excels at manipulating data through field-based processing, arithmetic calculations, pattern matching using regular expressions, and generating formatted reports.

The examples provided showcase just a fraction of awk’s capabilities. Its flexibility and robustness make it an essential tool for data manipulation, reporting, and text-processing tasks in Unix/Linux systems, catering to the needs of developers, system administrators, and data analysts working within the command-line environment.

Conclusion:
In conclusion, the awk command remains an indispensable tool in Unix/Linux systems for its data-processing capabilities. Its ability to process text or data by defining patterns and actions enables users to perform intricate operations efficiently from the command line. By leveraging awk’s concise syntax and powerful features, users can effectively manipulate data, generate reports, and automate various text-processing tasks, making it a go-to utility for developers, system administrators, and data analysts.

FAQs (Frequently Asked Questions) about the awk command in Unix/Linux with Examples:

Here are some FAQs related to awk command in Unix/Linux.

1. How does awk work?
awk processes data by defining patterns and corresponding actions. It scans input text or data, matches patterns, and performs actions on the matched content.

2. What are some common awk functionalities?
awk can perform operations such as searching for patterns using regular expressions, splitting fields based on delimiters, performing arithmetic or string operations, and generating formatted reports.

3. How can I use awk to print specific columns from a file?

awk '{print $2, $4}' file.txt

This command prints the second and fourth columns from file.txt.

4. Can awk perform arithmetic operations?
Yes, awk can perform arithmetic operations on data. For instance:

echo "5 10" | awk '{print $1 + $2}’

This command adds the numbers 5 and 10 and prints the result.

5. How does awk handle field separation in text files?
By default, awk separates fields based on whitespace (spaces or tabs). Users can define custom field separators using the -F option.

awk -F':' '{print $1}' /etc/passwd

This command uses : as the field separator to extract the first field (username) from the /etc/passwd file.

6. Can awk be used with conditions?
Yes, awk allows users to apply conditions for pattern matching and executing specific actions based on those conditions. For example:

awk '$3 > 100 {print $1}' file.txt

This command prints the first column from file.txt if the value in the third column is greater than 100.

Leave a Reply

Your email address will not be published. Required fields are marked *