thecoderworld
  • Programming
  • Security
  • Tech
  • Open Source
  • How To
  • Lists
  • Windows 11
  • Chromebook
  • Linux
No Result
View All Result
thecoderworld
  • Programming
  • Security
  • Tech
  • Open Source
  • How To
  • Lists
  • Windows 11
  • Chromebook
  • Linux
No Result
View All Result
thecoderworld
No Result
View All Result

Home > Linux > How to Use the Awk Command in Linux

How to Use the Awk Command in Linux

Nilam Shaw by Nilam Shaw
May 15, 2022
in How To, Linux
Reading Time: 4 mins read
0
How to Use the Awk Command on Linux
Share on FacebookShare on Twitter

AWK is a scripting language that analyzes text fields. It is widely used for processing data files containing lines and columns. AWK was first introduced in the 1970s. It has given the name based on the surnames of its developers: Alfred Aho, Peter Weinberger, and Brian Kernighan.

The basic format of AWK command is awk ‘pattern {action}’ input-file > output-file. Here actually, if the pattern is present in a line of the input file, awk will apply the action to the line and will write the resulting line to the output file.

RelatedPosts

Best Screen Recorders for Ubuntu

5 Best Screen Recorders for Ubuntu

May 20, 2022
How to Install VLC Media Player on Ubuntu

How to Install VLC Media Player on Ubuntu 22.04 LTS

May 20, 2022

How to Use the Awk Command in Linux

The awk command is present by default in modern Linux systems. So, no need to install it. Here we have discussed some points on how to use the awk command in Linux. So, here we go:

1. Print Specific Columns

To print a specific column one should use the command. For example, if you want to print 2nd and 3rd, use the following command:

$ awk '{print $2 "\t" $3}' file.txt

In any case, if you want to print all the lines and columns in a file simply use the following command:

$ awk '{ print $0}' file.txt

2. Print Lines with Specific Pattern

$ awk '/variable_to_be_matched/ {print $0}' file.txt 

One can use the above syntax for printing lines with a specific pattern. For example, if you want to print lines having the letter ‘o’ you have to add simply add ‘o’ in place of “variable to be matched”.

$ awk '/o/ {print $0}' file.txt

3. Print Columns with Specific Pattern

$ awk ' /a/ {print $3 "\t" $4}' file.txt

This command will print the 3rd and 4th columns that have the ‘a’ letter.

4. Built-in variables

For a better analysis of data, awk comes with several variables. Here, we described only a few of them.

NR: NR commands keep the current counts of the number of input records. To count the number of the input file, one can use the command:

$ awk ‘{print NR,  $0}’ file.txt

NF: The NF variable keeps the count of fields for each record.

$ awk ‘{print NF,  $0}’ file.txt

This command prints the last column of the table. One can also print the second last column of the table by using the command

$ awk ‘{print $2,  $(NF -1) }’ file.txt

ORS: ORS stands for output record separator. It separates every line of the input file desirably.

$ awk ‘ BEGIN {ORS = “:” } {print $0}’ file.txt

OFS: OFS stands for output field separator. It separates every field in the desired output format.

$ awk ‘ BEGIN {OFS = “==” } {print $1, $2}’ file.txt

5. Arithmetic Operations

If the table contains numbers, the awk command can be used to perform arithmetic operations.

$ awk ‘{print $1 + $3 }’ file.txt

6. Saving Output

To save the output results, one can use the redirection operator “>”. Moreover, you can verify the result using the cat command.

$ cat output.txt 

Awk command in Linux helps the programmer to write a useful program. Here we have discussed only a few commands. Hope you have liked the article and found it useful. If you have any suggestions, then leave them in the comment section below.

Previous Post

10 Handy Ubuntu Keyboard Shortcuts You Should Know

Next Post

How to Upgrade to Ubuntu 22.04 LTS

Nilam Shaw

Nilam Shaw

I am a passionate writer, I am interested in learning new facts about technology, computer, and love to read about them.

Recommended Posts

Best Screen Recorders for Ubuntu
Linux

5 Best Screen Recorders for Ubuntu

May 20, 2022
How to Install VLC Media Player on Ubuntu
How To

How to Install VLC Media Player on Ubuntu 22.04 LTS

May 20, 2022
How to Change Desktop Wallpaper in Ubuntu
How To

How to Change Desktop Wallpaper in Ubuntu

May 19, 2022
How To

How to Change Screen Resolution in Ubuntu

May 18, 2022
Why Linux Users are Going Crazy over WireGuard
Linux

Why Linux Users are Going Crazy over WireGuard?

May 17, 2022
What is Linux Operating System
Linux

What is Linux Operating System?

May 15, 2022

Leave a Reply Cancel reply

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

Stay Connected

  • 1.3k Fans
  • 702 Followers
  • 55 Followers
  • 24.1k Subscribers

Recent Posts

Best Screen Recorders for Ubuntu
Linux

5 Best Screen Recorders for Ubuntu

May 20, 2022
Linux

Top 5 Video Editing Software for Linux in 2021

May 20, 2022
thecoderworld

© 2018 - 2022 thecoderworld

Navigate Site

  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer
  • Advertise
  • Career

Follow Us

No Result
View All Result
  • Programming
  • Security
  • Tech
  • Open Source
  • How To
  • Lists
  • Windows 11
  • Chromebook
  • Linux

© 2018 - 2022 thecoderworld