Reputation: 167
I need to write a bash script that will take in input (a file) from the command line which contains a list of files inside. I then need to open those files inside the file and read it word by word keeping a count of the occurrences of each word in all the files in the list. So far it is printing out the list of files inside the file, but also the file itself. This is what I have so far. I am new to bash scripts so I am not sure how to do this. I appreciate any help. Thanks
#!/bin/bash
wordArray=()
countArray=()
INPUT="$1";
if [ -f "$INPUT" ]
then
find $name -type f
echo "$name";
else
echo "$INPUT is not a file!";
fi
Upvotes: 0
Views: 443
Reputation: 63892
To count the occurences of all words in all files from the list in one file, you can use:
xargs grep -hoP '\b\w+\b' < file_with_list | sort | uniq -c
Example:
file list.txt
test1.txt
test2.txt
test1.txt
hello world
test2.txt
hello word hello again
running:
xargs grep -hoP '\b\w+\b' < list.txt | sort | uniq -c
prints
1 again
3 hello
2 word
Caveats:
list.txt
can't contain spaces...Upvotes: 2
Reputation: 63892
Try:
#!/bin/bash
err() { echo "$@" >&2; return 1; }
printwords() {
echo "Wordcounts in the: $@"
for i in "${!words[@]}"
do
echo "$i => ${words[$i]}"
done
}
input="$1"
[[ -n $input ]] || err "Usage: $0 filename" || exit 1
[[ -f $input ]] || err "File $input doesn't exists" || exit 2
declare -A words
while read -r file
do
while read -r word
do
let words[$word]++
done < <(grep -oP '\b\w+\b' "$file")
done < "$input"
printwords "$(cat "$input" | xargs)"
Upvotes: 0
Reputation: 603
I think you are looking for something like this instead of find.
for name in $INPUT; do echo $name; done
this will print all files in your $INPUT file, of course you can do anything else in that loop.
Upvotes: -1