awk's Examples

print every line of data from the specified data

$ awk '{print}' employee.txt

print the lines which match the given pattern

$ awk '/manager/ {print}' employee.txt

splits the record

$ awk ' {print $1, $4}' employee.txt

use of NR buikt in variables .display line number

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

using NF built in variable displays last field

$ awk '{print $1, $NF}' employee.txt

displays line from 3 to 6

$ awk 'NR==3, NR==6 {print NR, $0}' employee.txt

displays line from 3 to 6

$ awk 'NR==3, NR==6 {print NR, $0}' employee.txt

NF should be 0 not less than and the user have to print the line number

$ awk 'NF < 0' geeksforgeeks.txt

find the length of the loongest line present in the file

$ awk '{if (length ($0)> max) max = length ($0)} END {print max}' geeksofgeeks .txt

to count the lines in a file

$ awk 'END {print NR}' geeksforgeeks.txt

to find or check for anby string in particular column

$ awk '{if($3=="B6") print $0;}' geeksforgeeks.txt

print a line that starts with specific ltter.

the ^ arrow indicates the beginning of the line and then the letter you want a line to star with

$ awk"/^0/' infrmation.txt

this prints out the lines that end in a0 and the symbol $ is used after a charcter to signify how a line will end.

$ awk '{0$}' information.txt

! is used as a not . it selects the lines that dont end in a 0

$ awk '! /o$/' information.txt

abbove seperate will seperate the output with (- )symbol

awk 'BEGIN {ORS="-"} {print $0}' student.txt

print ecer line of data

$ awk '{print} employee.txt

print ecer line of data

$ awk '{print} employee.txt

print the lines that match with the given pattern

$ awk '/manager/ {print}' employee.txt

print the lines that match with the given pattern

$ awk '/manager/ {print}' employee.txt

display line number

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

display last field

$awk'{print $1, $NF}' employee.txt

display the lines from 3 gto 6

$ awk 'NR==3, NR==6 {print NR, $0}'employee.txt

print any non empty line if present

$ awk ' NF<0' geeksforgeeks.txt

find the length of longest line present in the file

$ awk '{if length ($0)> max max = length ($0} END {print max}' geeksforgeeks .txt

count the lines in a file

$ awk 'END {print NR}' geeksforgeeks.txt

printing lines more than 10 linesd

$ awk 'length ($0)>10' geeeksforgeeks.txt

selects the that strts with o and  ^ indicates the beginninf of the line

$ awk '/^0/' information.txt

The " ! " is used as a not. it selects the line that dont end with o

$ awk '! /0$/' information.txt

it implies the number of arguments provided the command line.

$awk 'begin {print arguments=", ARGC' one two three four

it represents the cnversion format for numbers.

$ awk 'BEGIN {print "conversion format=",CONVFMT}'

it is associative array of environmet variables

$ awk 'BEGIN { print  ENVIRON "user" }'

prints file name 

$ awk 'END { print filename}'  marks.txt

it represents the outpput format numberand its default number %6g

awk 'BEGIN {print OFMT=OFMT}'

it represents the output field seperator. the default seprator is space

$ awk 'BEGIN { print"OFS=OFS"} |cat -vte

it represents the ouput record seperator. default is a new line

$  awk 'BEGIN { print ORS=ORS }| cat vte

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us