Thursday 8 August 2019

AWK command in Linux/Unix

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

Print Lines with More or less than a No. of Characters

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.

0 comments:

Post a Comment