sanity check file to produce binary answer

2

What Unix command(s) can I use to determine the existence of each of several words and the non-existence of each of several words which will produce a binary answer?

For example: I want to be sure that a file has the word "foo" listed 6 times and "bar" listed 8 times. I also want to be sure that the file has neither "fizz" nor any "buzz"

Ocasta Eshu

Posted 2012-06-13T01:12:37.117

Reputation: 778

Answers

1

There may be more elegent ways to do this, but this will do the trick, for a file called test:

if [ `grep -c bar test` == 8 ] && [ `grep -c foo test` == 6 ] && [ `grep -c fizz test` == 0 ] && [ `grep -c buzz test` == 0 ] ; then  echo "yes" ; else echo "no" ; fi

Paul

Posted 2012-06-13T01:12:37.117

Reputation: 52 173

This will fail when words appear multiple times on the same rows, since grep -c only returns number of matching rows, not word counts. echo "test test" | grep -c test will return 1. – Daniel Andersson – 2012-06-13T05:42:33.657

@DanielAndersson Good point Ocasta, can you add more information about the data structure to your question? – Paul – 2012-06-13T05:48:13.713

@Paul, I'm trying to build a sanity check for a derived text file. It may be that the same word appears multiple times on each line but if my understanding of how this particular text file is built is correct then it is a safe bet that the file is sane if each of the words are mentioned at least once. – Ocasta Eshu – 2012-06-25T02:13:00.967

So I guess a more relevant question would be how do I get a binary response if foo and bar exist an unknown number of times (i just want to be sure they are in the file)? – Ocasta Eshu – 2012-06-25T02:36:08.910

Changing the if to if [ grep -c bar test -gt 0 ] && [ grep -c foo test -gt 0 ]` would probably do it... – Paul – 2012-06-25T03:11:27.760