Awk Print Tutorial With Examples – POFTUT

Awk Print Tutorial With Examples


Awk is a stream type programming language. Awk can edit given text ant provided new text according to instructions. We will look popular usages of Awk in this  tutorial. There are alternative awk implementations like below

  • Nawk
  • Gawk

Awk Syntax

Awk is line oriented so by default provided text is processed line by line. Awk is generally used to search a pattern and make actions against it. We can call this pattern-action language

$ awk '/search_pattern/ {action;action;}' file
  • search_pattern used to find text int file
  • if search patterns found code between {action} will run.

Awk Flow

Awk have logic how to process provided text.  Awk will follow the steps below.

  1. Execute Awk Commands from BEGIN block
  2. Read line from file or standard  input
  3. Execute Awk commands on line
  4. If not end of file go step 2
  5. Execute Awk commands from END block

Example Text File

We will use following text file named fruits in this tutorial.

apple   1       good    JAN 
grape   5       bad     DEC 
banana  2       good    MAR 
melon   10      bad     JUN
Example Text File
Example Text File

Print Line by Line

We will make an example about how Awk works. In the following example Awk simply read lines from file fruits.txt and write to the standard output. We can see here the logic of Awk a bit.

$ awk '{print;}' fruits.txt
Print Line by Line
Print Line by Line

Print Matched Lines

Awk usage is not printing files line by line. To create complex usage scenarios there should be some match function. Match is implemented with / like below by providing text file.

$ awk '/bad/' fruits.txt
  • /bad/ is our search term
LEARN MORE  How To Get Ram Size In Linux?
Print Matched Lines
Print Matched Lines

Multi Term Match

Previous examples works with single term match. But there are some situations we will need multi term match. We can provide multi term match like below. The single quote will not closed until the

$ awk '/JAN/ 
> /JUN/' fruits.txt
Multi Term Match
Multi Term Match

Print Columns

As we know some text will be column based. We can only print specified columns. by using Awk variables.

$ awk '{print $2}' fruits.txt
  • We specify with {print } we want to use print function.
  • $2 is the column of the text. Columns are separated by tabs
Print Columns
Print Columns

BEGIN and END statements

As stated previously in the workflow of the Awk there are BEGIN and END steps those executed accordingly. We can print beginning and end statements.

$ awk 'BEGIN{print"This is beginning"}  
> {print $1 $2}  
> END{print"This is end"}' fruits.txt
BEGIN and END statements
BEGIN and END statements

Filter Comparing Columns

Another useful feature is  using comparison filters to select only matched lines according to the provided values and operators. We want to get fruits those is bigger than 5. Here is how can we achieve this.

$ awk '$2<5' fruits.txt
  • $2 is the column we want to compare
  • > 5 is the compare condition
Filter Comparing Columns
Filter Comparing Columns

Filter Selecting Columns

While filtering rows the match can be specified for specific column. Following example will look column 1 for ban text. We can search for a specific term to specified column.

$ awk '$1 ~/ban/' fruits.txt
  • $1 is the column our filter is based
  • ~/ismail/ is term we want to filter
Filter Selecting Columns
Filter Selecting Columns

Print The Number Of Lines

We can count lines numbers by using variables. We will look names that contain “i” and count them.

$ awk 'BEGIN{count=0} 
/i/ {count++} 
END{print count;}'
  • We set count with BEGIN statement
  • we increment count++ for each match
  • We print  count with {print count;}
LEARN MORE  How To Restart CentOS, Fedora, RedHat?
Print The Number Of Lines
Print The Number Of Lines

Reading Awk Script From File

Up to now we have get Awk program directly. But Awk utility provides the ability to read from file with parameter -f .

$ awk -f myawk users
  • -f reads script from myawk and runs  on users

Using Regex and Glob Expressions

Standard text expression is powerful for most of the operations. But sometimes this will not enough. There is support for glob and regex expressions. Awk by default provides glob search. But providing regex search is possible too

$ awk '/[10]/' fruits.txt
  • [34] is our regular expression means find lines that contains 3 or 4
Using Regex and Glob Expressions
Using Regex and Glob Expressions

Print Columns Between Specified Range

We can provide the column which will be not printed.

$ awk '{$1=""; print $0}' fruits.txt
  • $1=”” makes column number 1 empty
  • print $0 prints corrent column
Print Columns Between Specified Range
Print Columns Between Specified Range

Variables

Sometimes we need to hold some temporary values through execution of the Awk. The solution is using variables. Variables are defined before Awk script execution and used through the execution of the script.

$ awk -v limit=5 '$2<limit' fruits.txt

Version

Sometimes Awk version is important it can be printed like below.

$ awk --version
Version
Version

 

Awk Print Tutorial With Examples Infografic

Awk Print Tutorial With Examples Infografic
Awk Print Tutorial With Examples Infografic

Leave a Comment