0

I can do the following using a for loop

    for f in *.txt; do grep 'RINEX' $f |wc -l; done

Is there any possibility to get an individual file report by running one liner?

Meaning that I want to grep & wc one file at the time in a similar fashion like

    grep 'RINEX' *.txt

UPDATE:

    grep -c 'RINEX' *.txt

returns the name of each file and its corresponding number of occurrences. Thx @Evert

AjanO
  • 433
  • 1
  • 9
  • 20
  • How does `grep 'RINEX' *.txt` operate one file at a time. It's actually your for-loop that works on one file at a time. (Btw: `$f.txt` or just `$f`?) –  Mar 11 '16 at 02:34
  • Or are you looking for the count option to grep? "-c, --count: Only a count of selected lines is written to standard output." –  Mar 11 '16 at 02:35
  • Thx. I corrected the first one. The 2nd answer was the expected behaviour. – AjanO Mar 11 '16 at 05:32

1 Answers1

0

grep is not the right tool for this task.

grep does line based match, e.g. line grep 'o' <<< "fooo" will return 1 line. however we have 3 os.

This one-liner should do what you want:

awk -F'RINEX' 'FILENAME!=f{if(f)print f,s;f=FILENAME;s=0}
               {s+=(NF-1)}
               END{print f,s}' /path/*.txt
Kent
  • 189,393
  • 32
  • 233
  • 301