Tutorial

AWK command in Linux/Unix

Published on August 3, 2022
AWK command in Linux/Unix

AWK is suitable for pattern search and processing. The script runs to search one or more files to identify matching patterns and if the said patterns perform specific tasks. In this guide, we take a look into AWK Linux command and see what it can do.

What Operations can AWK do?

  • Scanning files line by line
  • Splitting each input line into fields
  • Comparing input lines and fields to patterns
  • Performing specified actions on matching lines

AWK Command Usefulness

  • Changing data files
  • Producing formatted reports

Programming Concepts for awk command

  • Format output lines
  • Conditional and loops
  • Arithmetic and string operations

AWK Syntax

awk options 'selection _criteria {action }' input-file > output-file

To demonstrate more about AWK usage, we are going to use the text file called file.txt awk command example file 1st column => Item, 2nd column => Model 3rd column => Country 4th column => Cost

Awk Command Examples

Printing specific columns

To print the 2nd and 3rd columns, execute the command below.

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

Output Awk Print Second And Third Column

Printing all lines in a file

If you wish to list all the lines and columns in a file, execute

awk ' {print $0}' file.txt

Output Awk Print All Lines

Printing all lines that match a specific pattern

if you want to print lines that match a certain pattern, the syntax is as shown

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

For instance, to match all entries with the letter ‘o’, the syntax will be

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

Output awk linux command matching all lines To match all entries with the letter ‘e’

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

Output Awk Matching Lines With E

Printing columns that match a specific pattern

When AWK locates a pattern match, the command will execute the whole record. You can change the default by issuing an instruction to display only certain fields. For example:

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

The above command prints the 3rd and 4th columns where the letter ‘a’ appears in either of the columns Output Awk Matching Column

Counting and Printing Matched Pattern

You can use AWK to count and print the number of lines for every pattern match. For example, the command below counts the number of instances a matching pattern appears

awk '/a/{++cnt} END {print "Count = ", cnt}' file.txt

Output Count Columns Matching a pattern

AWK has a built-in length function that returns the length of the string. From the command $0 variable stores the entire line and in the absence of a body block, the default action is taken, i.e., the print action. Therefore, in our text file, if a line has more than 18 characters, then the comparison results true, and the line is printed as shown below.

awk 'length($0) > 20' file.txt

Output Print Lines With More Or Less Characters

Saving output of AWK to a different file

If you wish to save the output of your results, use the > redirection operator. For example

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

You can verify the results using the cat command as shown below

cat output.txt

Output Awk Redirect Output

Conclusion

AWK is another simple programming script that you can use to manipulate text in documents or perform specific functions. The shared commands are a few or the many you are yet to know or come across.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Sr. Technical Writer@ DigitalOcean | Medium Top Writers(AI & ChatGPT) | 2M+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 24, 2020

I want to replace the strings “www.example.com” with https://linuxbuz.com in all files inside /etc directory. How can i achieve this with awk command? Thanks in advance.

- sima chavda

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more