Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk matching - newbie problem 2

Status
Not open for further replies.

SotonStu

Programmer
May 7, 2003
33
0
0
GB
Hi all,
I have a file containing a list of words, one word per line, eg:

france
germany
usa
england

I want to count the occurrences of each of these words in another file. At the moment though I can't even get the awk script to find any of the words, let alone their count.

This is my shell script:

#!/bin/sh
cat words | \
while read line
do
awk '/$line/{print $line}' report.txt
done

i thought that this would print out the matching word if it was found within the file reports.txt but nothing is ever printed out, where am I going wrong?

Many thanks for your help.
 
I want to count the occurrences of each of these words in another file
Like this ?
awk '
NR==FNR{a[$1]=0;next}
{ for(i=1;i<=NF;++i)if($i in a)++a[$i] }
END{ for(i in a)print i,a }
' words report.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
# Reading first file?
ARGV[1]==FILENAME { a[$1] ; next }

# Now reading 2nd file.
{ for ( i=1; i<=NF; i++ )
    if ( $i in a )
      a[$i]++
}

# After all files have been read.
END {
  for (k in a)
    print k, a[k]
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top