Awk Print Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/awk-print-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

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

  • 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

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

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.

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.

LEARN MORE  Search Python Packages with Pip

  • /bad/ is our search term
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

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.

  • 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.

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.

  • $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.

LEARN MORE  How to Use 'fuser' to Match User and Process

  • $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.

  • We set count with BEGIN statement
  • we increment count++ for each match
  • We print  count with {print count;}
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 .

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

  • [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.

  • $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.

LEARN MORE  How To Start, Stop, Restart Networking On Linux?

Version

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

Version

Version

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/awk-print-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)