Filtering the Columns

BilalBilal
1 min read

Using awk command we can filter the columns and show the result. It works on CSV file.

Also it checks the pattern and print the line on the basis of column wise.

Syntax: awk '/keyword-name'/ filename

Eg: ubuntu@ip-172-31-25-118:~$ awk '/TRACE/' application.log

Output:

03/22 08:51:06 TRACE :...read_physical_netif: Home list entries returned = 7

03/22 08:51:06 TRACE :..entity_initialize: interface 129.1.1.1, entity for rsvp allocated and initialized

03/22 08:51:06 TRACE :..entity_initialize: interface 9.37.65.139, entity for rsvp allocated and

I want 1st column, 2nd column and 4th column

Syntax: awk '/keyword-name/ {print $Column1, $Column2, Column 4} filename

Eg: ubuntu@ip-172-31-25-118:~$ awk '/TRACE/ {print $1,$2,$4}' application.log

Output:

03/22 08:51:06 :...read_physical_netif:

03/22 08:51:06 :..entity_initialize:

03/22 08:51:06 :..entity_initialize:

NR is a built in variable that defines Total No of Records

awk '{print NR, $0}' filename -> It prints the content of the line along with line number.

Eg: Suppose you have a file named example.txt with the following content:

Hello

World

This is

an example

file

awk '{ print NR, $0 }' example.txt

Output:

1 Hello

2 World

3 This is

4 an example

If you want to print 1st 5 lines of a code

awk 'NR <=5' filename

If you want to print line no from 10 to line no 20

awk 'NR >= 10 && NR <= 20' filename

0
Subscribe to my newsletter

Read articles from Bilal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bilal
Bilal