0% found this document useful (0 votes)
131 views

Awk Script PDF

This document contains 9 questions related to using awk to analyze text files. It provides sample text files and solutions to demonstrate: 1) Counting unique words and occurrences in a file with multiple words per line 2) Counting the number of times "Linux" appears across files in the current directory and subdirectories 3) Printing lines that match a specific pattern

Uploaded by

Jiten
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
0% found this document useful (0 votes)
131 views

Awk Script PDF

This document contains 9 questions related to using awk to analyze text files. It provides sample text files and solutions to demonstrate: 1) Counting unique words and occurrences in a file with multiple words per line 2) Counting the number of times "Linux" appears across files in the current directory and subdirectories 3) Printing lines that match a specific pattern

Uploaded by

Jiten
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/ 3

Question 1:

Write A Shell Script To Find Out The Unique Words In A File And Also Count The Occurrence Of Each Of These
ords.
Note: It may possile each Line Has Multiple Words.

Solution:

Assume File:
cat animal.txt
tiger bear
elephant tiger bear
bear

Command: $ awk '{for(i=1;i<=NF;i++)a[$i]++;}END{for(i in a){print i, a[i];}}' animal.txt

Question 2:
Write A Shell Script To Get The Total Count Of The Word “Linux” In All The “.Txt” Files And Also Across Files
resent In Subdirectories.

Solution:

$ find . -name *.txt -exec grep -c Linux '{}' \; | awk '{x+=$0;}END{print x}'

_____________________________________________________________________________________________
_______________________________________________________
To complete further Questions, create test.txt file with below texts:

test.txt
one two three four five six
John 246810 team01 UK Birmingham FTE
Paul 135790 team02 UK Glasgow FTE
Marcus 049583 team03 DE Bremen PTE
Foxy 903485 team01 UK Aston PTE

Question 3: awk '/team01/ { print $0}' test.txt

Questionn 4: awk '/team01/ { print $1,"-",$2,"-",$3}' test.txt

Question 5: awk '/FTE$/ { print $0 }' test.txt

Question 6: awk 'BEGIN{


x=1;
while(1)
{
print "Count = ",x;
if ( x==10 )
break;
x++;
}}'
Question 7:

Create below awk script, then run on test.txt file.


#!/usr/bin/awk -f
#
# Test awk script
#

BEGIN {
print "--- I am a test awk file ---"
count=0
}

{
if ($3 =="team01") {
print "team01 members found: "$1,"-",$3
count=count+1
}
}

END {
print "------------------------------"
printf("\tTotal Number of Records Processed:\t%d\n", NR)
printf("\tNumber of team01 members found :\t%d\n", count)
}

Question 8:

Create text file as a 'mail-list'

Amelia 555-5553 [email protected] F


Anthony 555-3412 [email protected] A
Becky 555-7685 [email protected] A
Bill 555-1675 [email protected] A
Broderick 555-0542 [email protected] R
Camilla 555-2912 [email protected] R
Fabius 555-1234 [email protected] F
Julie 555-6699 [email protected] F
Martin 555-6480 [email protected] A
Samuel 555-3430 [email protected] A
Jean-Paul 555-2127 [email protected] R

Run this command:

awk '
> BEGIN { print "Analysis of \"li\"" }
> /li/ { ++n }
> END { print "\"li\" appears in", n, "records." }' mail-list

Question 9: Remove duplicacy


{
if (data[$0]++ == 0)
lines[++count] = $0
}

END {
for (i = 1; i <= count; i++)
print lines[i]
}

You might also like