0

I have a lot of files in several subfolders that include a string type="abc_ - I'm not too sure how many variants of abc_ there is - so I want to know how many occurrences of the unknown type="abc_ are in within my files.

I'd hope to get something that is like line containing

"type="abc_0815 found 50 times
"type="abc_0816 found 32 times
...

and so on.

If I do like that:

grep -rni 'type="abc_' * | wc

I already know its 14905 occurrences with type="abc_.

Can somebody help me on that?

1
  • True that - sorry. I have a lot of files cotainting that string. Those files a located in a lot of subfolders.
    – user498432
    Oct 25, 2021 at 12:00

3 Answers 3

1

This uses find to get a file listing passes it to cat and parses the output with awk:

find . -type f |
xargs -I xx cat "xx" | awk '/type="abc_/{
  for(i=1;i<=NF;i++){
    if($i~/type="abc_/){ d[$i]++ } } } 
  END{ for(i in d){ print i"\tfound",d[i],"times." } }'

type="abc_4  found 1 times.
type="abc_3  found 2 times.
type="abc_6  found 1 times.
type="abc_2  found 2 times.
type="abc_10 found 3 times.
type="abc_5  found 1 times.

It uses find instead of a simple cat * to be more flexible what to search for.

xargs .. cat | .. can be shortened with find . -type f -exec cat {} + | awk ..

1
  • Thanks Sir! This exactly does the trick! <3 Thank you so much!
    – user498432
    Oct 25, 2021 at 12:30
0

just add -c flag to have grep count it for you.

In case there is too many 0 results, filter out with awk

  grep -rnic 'type="abc_' * | awk -F: '$NF>0' 
1
  • Thats cool - it shows me how many findings there were in those files. Is it possible to get also the exact finding? like found 'type="abc_0815" 2 times and so on?
    – user498432
    Oct 25, 2021 at 12:02
0

Try similar this:

grep -rni 'type="abc_' * |sed -n "s/.*\(abc_[0-9]*\).*/\1/p"|uniq -c| sed  "s/\(.*\)\(abc.*\)/\2 found \1 times/"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.