0% found this document useful (0 votes)
362 views3 pages

Awk Script Test 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)
362 views3 pages

Awk Script Test 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 amelia.zodiacusque@gmail.com F


Anthony 555-3412 anthony.asserturo@hotmail.com A
Becky 555-7685 becky.algebrarum@gmail.com A
Bill 555-1675 bill.drowning@hotmail.com A
Broderick 555-0542 broderick.aliquotiens@yahoo.com R
Camilla 555-2912 camilla.infusarum@skynet.be R
Fabius 555-1234 fabius.undevicesimus@ucb.edu F
Julie 555-6699 julie.perscrutabor@skeeve.com F
Martin 555-6480 martin.codicibus@hotmail.com A
Samuel 555-3430 samuel.lanceolis@shu.edu A
Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu 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