100% found this document useful (1 vote)
136 views

AWK Part-3

The document contains examples demonstrating various awk commands and operations on text files. It shows how to select, filter, modify and analyze data using awk's pattern matching, field splitting, arithmetic operations and built-in variables. Regular expressions are used to match patterns. Output is formatted using printf. The examples calculate averages, counts, file metadata, word frequencies and more.

Uploaded by

nagarjuna0595
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
136 views

AWK Part-3

The document contains examples demonstrating various awk commands and operations on text files. It shows how to select, filter, modify and analyze data using awk's pattern matching, field splitting, arithmetic operations and built-in variables. Regular expressions are used to match patterns. Output is formatted using printf. The examples calculate averages, counts, file metadata, word frequencies and more.

Uploaded by

nagarjuna0595
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Example 1

words.txt
storeroom
tree
cup
store
book
cloud
existence
ministerial
falcon
town
sky
top
bookworm
bookcase
war

$ awk 'length($1) > 5 {print $0}' words.txt


storeroom
existence
ministerial
falcon
bookworm
bookcase

$ awk 'length($1) == 3' words.txt


cup
sky
top
war

$ awk '!(length($1) == 3)' words.txt


storeroom
tree
store
book
cloud
existence
ministerial
falcon
town
bookworm
bookcase

$ awk '(length($1) == 3) || (length($1) == 4)' words.txt


tree
cup
book
town
sky
top
war
$ awk 'length($1) > 0 {print $1, "has", length($1), "chars"}' words.txt
storeroom has 9 chars
tree has 4 chars
cup has 3 chars
store has 5 chars
book has 4 chars
cloud has 5 chars
existence has 9 chars
ministerial has 11 chars
falcon has 6 chars
town has 4 chars
sky has 3 chars
top has 3 chars
bookworm has 8 chars
bookcase has 8 chars
war has 3 chars
Example 2:

scores.txt
Peter 89
Lucia 95
Thomas 76
Marta 67
Joe 92
Alex 78
Sophia 90
Alfred 65
Kate 46

$ awk '$2 >= 90 { print $0 }' scores.txt


Lucia 95
Joe 92
Sophia 90

$ awk '{sum += $2} END { printf("The average score is %.2f\n", sum/NR) }'
scores.txt
The average score is 77.56

Example 3

$ ls -l
total 132
drwxr-xr-x 2 jano7 jano7 512 Feb 11 16:02 data
-rw-r--r-- 1 jano7 jano7 110211 Oct 12 2019 sid.jpg
-rw-r--r-- 1 jano7 jano7 5 Jul 22 20:21 some.txt
-rw-r--r-- 1 jano7 jano7 226 Apr 23 16:56 thermopylae.txt
-rw-r--r-- 1 jano7 jano7 365 Aug 4 10:22 users.txt
-rw-r--r-- 1 jano7 jano7 24 Jul 21 21:03 words.txt
-rw-r--r-- 1 jano7 jano7 30 Jul 22 21:20 words2.txt

$ ls -l | awk '{print $6 " " $9}'


Feb data
Oct sid.jpg
Jul some.txt
Apr thermopylae.txt
Aug users.txt
Jul words.txt
Jul words2.txt

Example 4

users.txt
John Doe, gardener, London, M, 11/23/1982
Jane Doe, teacher, London, F, 10/12/1988
Peter Smith, programmer, New York, M, 9/18/2000
Joe Brown, driver, Portland, M, 1/1/1976
Jack Smith, physician, Manchester, M, 2/27/1983
Lucy Black, accountant, Birmingham, F, 5/5/1998
Martin Porto, actor, Los Angeles, M, 4/30/1967
Sofia Harris, interpreter, Budapest, F, 8/18/1993

$ awk 'BEGIN {FS=","} {print $3}' users.txt


London
London
New York
Portland
Manchester
Birmingham
Los Angeles
Budapest

$ awk -F, '$4 ~ "F" {print $1}' users.txt


Jane Doe
Lucy Black
Sofia Harris
$ awk -F, '{print $NF, $(NF-1)}' users.txt
11/23/1982 M
10/12/1988 F
9/18/2000 M
1/1/1976 M
2/27/1983 M
5/5/1998 F
4/30/1967 M
8/18/1993 F

males_females.awk
{
if ($4 ~ "M") {

m++
} else {

f++
}
}

END {
printf "users: %d\nmales: %d\nfemales: %d\n", m+f, m, f
}

$ awk -F, -f males_females.awk users.txt


users: 8
males: 5
females: 3
Example 5

AWK regular expressions

Regular expressions are often applied on AWK fields. The ~ is the regular
expression match operator. It checks if a string matches the provided regular
expression.

$ awk '$1 ~ /^[b,c]/ {print $1}' words.txt


cup
book
cloud
bookworm
bookcase

In this command we print all the words that begin with b or c character. The
regular expression is placed between two slash characters.

$ awk '$1 ~ /[e,n]$/ {print $1}' words.txt


tree
store
existence
falcon
town
bookcase

This command prints all words that end with e or n.

$ awk '$1 ~ /\<...\>/ {print $1}' words.txt


cup
sky
top
war

The command prints all words that have three characters. The doc (.) stands
for any character and the \< \> characters are word boundaries.

$ awk '$1 ~ /\<...\>/ || $1 ~ /\<....\>/ {print $1}' words.txt


tree
cup
book
town
sky
top
war

We combine two condition with the or (||) operator. The AWK command
prints all words that have either three of four characters.

$ awk '$1 ~ /store|room|book/' words.txt


storeroom
store
book
bookworm
bookcase

With the alternation operator (|), we print fields that contain either of the
specified wores.

$ awk '$1 ~ /^book(worm|case)?$/' words.txt


book
bookworm
bookcase

echo a b c d | awk ’{ OFS = ":"; $2 = "" ; print ; print NF }’

Example 6
values.txt
2 3 1 34 21 12
43 21 11 2 11 33 12
43 72 91 90 32 14
34 87 22 12 75 2 42 13
75 23 1 42 41 94 4 32
21621314
53 13 52 84 14 14 63
3 2 5 76 31 45
$ awk 'NF == 6' values.txt
2 3 1 34 21 12
43 72 91 90 32 14
3 2 5 76 31 45

We print records that have six fields.

$ awk '{print "line", NR, "has", NF, "values"}' values.txt


line 1 has 6 values
line 2 has 7 values
line 3 has 6 values
line 4 has 8 values
line 5 has 8 values
line 6 has 8 values
line 7 has 7 values
line 8 has 6 values

calc_sum.awk
{
for (i = 1; i<=NF; i++) {

sum += $i
}

print "line", NR, "sum:", sum

sum = 0
}

$ awk -f calc_sum.awk values.txt


line 1 sum: 73
line 2 sum: 133
line 3 sum: 342
line 4 sum: 287
line 5 sum: 312
line 6 sum: 20
line 7 sum: 293
line 8 sum: 162
Example 7

A. Design a command “wishme” that will great you “good


morning”,”good afternoon”, according to current time.

date | awk ‘{
if($4 < 12) print ”good afternoon”
else print ”good morning” }’

B. Design a command “fags” thats will list the files and their ages, to
date.
ls –l | awk ‘BEGIN{
no [“Jan”]=1
no[“Feb”]=2
.
.
no[“Nov”]=11
no[“Dec”]=12
split(“’”`date`”’”,age)
}
{
m=no[age[2]]-no[$6]
d=age[3]-$7
if(d<0) { d=d+30;m--}
printf(“%s ➔ mon %d:%d\n”,$9,m,d)
}’

C. Design a command “word-freq” that will print the words and


number of occurrences of that word in the given text.
awk ‘{ for(i=1;i<=NF;i++) num[$i]++}
END {for(word in num) print word,num[word] }’ $*

You might also like