Reputation: 235
I wish to take names of two files as command line arguments in bash shell script and then for each word (words are comma separated and the file has more than one line) in the first file I need to count its occurrence in the second file. I wrote a shell script like this
if [ $# -ne 2 ]
then
echo "invalid number of arguments"
else
i=1
a=$1
b=$2
fp=*$b
while[ fgetc ( fp ) -ne EOF ]
do
d=$( cut -d',' -f$i $a )
echo "$d"
grep -c -o $d $b
i=$(( $i + 1 ))
done
fi
for example file1 has words abc,def,ghi,jkl (in first line ) mno,pqr (in second line)
and file2 has words abc,abc,def
Now the output should be like abc 2 def 1 ghi 0
Upvotes: 0
Views: 6252
Reputation: 20980
Another approach:
words=( `tr ',' ' ' < file1`) #split the file1 into words...
for word in "${words[@]}"; do #iterate in the words
printf "%s : " "$word"
awk 'END{print FNR-1}' RS="$word" file2
# split file2 with 'word' as record separator.
# print number of lines == number of occurrences of the word..
done
Upvotes: 1
Reputation: 785531
To read a file word by word separated by comma use this snippet:
while read -r p; do
IFS=, && for w in $p; do
printf "%s: " "$w"
tr , '\n' < file2 | grep -Fc "$w"
done
done < file1
Upvotes: 2