I am wondering if anyone has any idea how to use an awk within awk to read files and find a match which adds to count.
Say I am searching how many times the word crap appears in each files within a directory. How would i do that from the command prompt ...
thanks
awk can do it..
awk '/crap/{crap++}END{print crap}' filename
thanks vidyadhar85...
that works for one file. So I would have to do that for every file in the dirctory. I was hoping there was a way to do that for all the files in the directory with a single command from the prompt.
This should be sufficient, check the man page of grep.:
grep -c 'crap' *
Thanks Franklin52....
this was the command I used
ls -l | awk 'BEGIN {crap=0} {if (^[^d]) {awk '/crap/{crap++}' $8} END {print crap}'
I was hoping to be able to just print the total count ...
but kept getting a syntax error
how can u use awk in side awk.. you can't do so...
so u want total count of crap in a dir??
if you want the count no of crap from a dir use
awk '/crap/{crap[FILENAME]++}END{for(crapcount in A){print A[crapcount]" "crapcount}}' *
These solutions count the number of lines containing the character sequence "crap". However, the OP was looking for the number of word occurrences per file. I think what is needed is something like this:
for i in *; do printf "$i: "; grep -c "\bcrap\b" <(xargs -n1<$i); done