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.
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 1st column => Item, 2nd column => Model 3rd column => Country 4th column => Cost
To print the 2nd and 3rd columns, execute the command below.
awk '{print $2 "\t" $3}' file.txt
Output
If you wish to list all the lines and columns in a file, execute
awk ' {print $0}' file.txt
Output
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 To match all entries with the letter ‘e’
awk '/e/ {print $0}' file.txt
Output
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
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
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
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 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.
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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