0

I need to find the number of words in a file (1 word per line) that do not contain some set of letters. To do this I want to take the difference of grep -c '.' <file> and grep -c '<other_pattern>' <file>. I tried piping to bc with the following command echo "(grep -c '.' <file>) - (grep -c '<other_pattern>' <file>)" | bc. The result simply said syntax error. I was hoping someone had insight as what syntax is necessary for bc to accept the results of the grep calls. I need to do this in a single command as well.

Thanks in advance.

  • 3
    Does your system's grep not have the facility (-v, --invert-match) to invert the match? – steeldriver Feb 15 '16 at 18:31
  • Yes, actually. I realized just before seeing this. I had originally tried negating within the pattern and some other post on regex negating made me shy away from it. But using inverting matching solved my problem. – cheft Feb 15 '16 at 18:34
  • also - if you have a strict requirement for doing this in a single command, steeldriver's got it. (mine uses a built-in echo, two greps, and a bc) – Jeff Schaller Feb 15 '16 at 18:38
1

You're simply missing two dollar signs to enable command substitution:

echo $(grep -c '.' <file>) - $(grep -c '<other_pattern>' <file>) | bc

I also removed the quotes, since they aren't really necessary.

  • looks like I answered the problem that you asked, but steeldriver answered the question that you really had. (grep -cv) – Jeff Schaller Feb 15 '16 at 18:36
  • Ahh, thank you. I did ultimately get what I wanted with inverted matching but this is very useful as well – cheft Feb 15 '16 at 18:38

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.