15 Linux awk command examples,Enriching your command on Linux

By | January 30, 2017

awk means authors (“Aho, Weinberger, and Kernighan” )  of awk programming language.Awk mostly used for pattern search in multiple files and process the lines.It is not less than programming language. It has Variables,functions as so on.We can write awk scripts and awk commands too.

As everyone says,awk reads each line as record like row in table record.With the help of regular expressions we will try to extract exact word from the line of file.Regular expressions can be simply defined as sequence of characters which defines particular string format(formula to fetch one format/style of words).

15 linux awk command examples

15 linux awk command examples

Let’s go by the Simple Awk comand examples

Usage 1: Syntax of the “Awk”

[ SLTMachine: ] # awk 'script' slt_filename

“script” means you can mention any regular expression to filter contents in slt_filename.

Usage 2: print only first column using stdin.

[ SLTMachine: ] # awk ' {print $1} '

simply learn topics . com

simply learn java learn

In the above output ,it will read first word from the each line and print it,immediately.

[ SLTMachine: ]
simply
simply

Usage 3:Print complete file using awk.

[ SLTMachine: ] # awk '//{print}' /etc/passwd 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/bin/bash 
daemon:x:2:2:Daemon:/sbin:/bin/bash

The above command prints contents of complete file.

Usage 4:Print Employee records which has salary than 5000

[ SLTMachine: ] # awk '$3 > 3000' employee.txt 
SRINIVAS 101 20000 DIRECTOR 
RAJU 201 10000 MANAGER 
SRIDHAR 201 5000 LEAD 
KRUTHIKA 401 4000 PROGRAMMER

Here,we can fetch by using third argument ( $3 ) argument.

Usage 5:Print Employee records who has designation has "MANAGER"
[ SLTMachine: ] # awk '$4 == "MANAGER"' employee.txt
RAJU 201 10000 MANAGER

the above command fetches where it will try to match with 4th argument with “MANAGER”.

Usage 6: Writing awk script to find number of employees in department “Testing”

[ SLTMachine: ] # cat Database_employee
SRINIVAS 101 20000 DIRECTOR DEVELOPMENT
RAJU 201 10000 MANAGER TESTING
SRIDHAR 201 5000 LEAD TESTING
KRUTHIKA 401 4000 PROGRAMMER DEVELOPMENT

the above cat command displays the contents of input File “Database_employee”.

[ SLTMachine: ] # awk 'BEGIN { count=0;}
$5 ~ 
/DEVELOPMENT/ { count++; }
END { print "Development Team count =>",count;}'
Database_employee
Development Team count => 2

Usage 7:Print only lines when lenght of line is more than 30 characters.

[ SLTMachine: ] # awk 'length($0) > 30' text.txt
Usage 8: Print only lines when length of line is more than 100 characters.
 [ SLTMachine: ] # awk 'length($0) > 100' text.txt

Usage 9:Print only first three words of the file, by interchanging the position’s of it.

Check the input file contents of “Database_employee”

[ SLTMachine: ] # awk '{ print $3, $2, $1}' Database_employee
20000 101 SRINIVAS
10000 201 RAJU
5000 201 SRIDHAR
4000 401 KRUTHIKA

In the above ouput,I am printing third column first and second column second and first column in 3rd place.

Usage 10: How to write bigger “awk” commands,say we have multiple command to do.Then write program.awk and execute like below.

[ SLTMachine: ] # awk -f program.awk Database_employee

Usage 11: How to display number of columns in each row

[ SLTMachine: ] # awk '{print NF}' Database_employee
5
5
5
5

Usage 12: How to display column numbers from 1.

[ SLTMachine: ] # awk '{print NR}' Database_employee
1
2
3
4

Usage 13: How to display total numbers of lines in a file.

[ SLTMachine: ] # awk 'END {print NR}' Database_employee
4

Usage 14: Remove duplicates in a file without any sorting.

[ SLTMachine: ] # cat Database_employee
SRINIVAS        101     20000   DIRECTOR        DEVELOPMENT
RAJU            201     10000   MANAGER         TESTING
RAJU            201     10000   MANAGER         TESTING
RAJU            201     10000   MANAGER         TESTING
SRIDHAR         201     5000    LEAD            TESTING
KRUTHIKA        401     4000    PROGRAMMER      DEVELOPMENT
KRUTHIKA        401     4000    PROGRAMMER      DEVELOPMENT

Databse_employee is the source of file which has duplicates.

 

[ SLTMachine: ] # awk '!x[$0]++' Database_employee > withoutduplicates
[ SLTMachine: ] # cat withoutduplicates
SRINIVAS        101     20000   DIRECTOR        DEVELOPMENT
RAJU            201     10000   MANAGER         TESTING
SRIDHAR         201     5000    LEAD            TESTING
KRUTHIKA        401     4000    PROGRAMMER      DEVELOPMENT

Withouduplicates is result file,which contains no duplicates.

Usage 15: Let’s add all the numbers available in the third column of “Database_employee” file.

[ SLTMachine: ] # awk ‘{total += $3} END {print total}’ Database_employee

63000

if any new usages if you know on the linux awk command,feel free to post it on comments section and make it more discussion.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply