0% found this document useful (0 votes)
2 views

awk

AWK is a command-line tool in Linux/Unix for pattern searching and processing in files, allowing users to scan files, split lines into fields, and perform actions on matching patterns. The document outlines various operations that can be performed using AWK, including printing specific columns, counting matched patterns, and saving output to different files. It also provides syntax and examples to demonstrate the usage of AWK commands effectively.

Uploaded by

sagarmanikanta94
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

awk

AWK is a command-line tool in Linux/Unix for pattern searching and processing in files, allowing users to scan files, split lines into fields, and perform actions on matching patterns. The document outlines various operations that can be performed using AWK, including printing specific columns, counting matched patterns, and saving output to different files. It also provides syntax and examples to demonstrate the usage of AWK commands effectively.

Uploaded by

sagarmanikanta94
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

AWK stands for Aho, Weinberger, and Kernighan, the surnames of its creators.

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.txtawk command example
file1st 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


OutputAwk 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


OutputAwk 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


Outputawk linux command matching all linesTo match all entries with the letter ‘e’

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


OutputAwk 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 OutputAwk 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


OutputCount 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


OutputPrint 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

You might also like