0

I have three files foo1.txt, foo2.txt and foo3.txt, which contain the following lines

# foo1.txt
JOBDONE

and

# foo2.txt
Execution halted

and

# foo3.txt
Execution halted
JOBDONE

I have been able find the ones with both JOBDONE and Execution halted using:

find ./foo*.txt | xargs grep -l 'Execution halted' | xargs grep -l "JOBDONE"

But have not been able to find those files which have JOBDONE or Execution halted but not both. I have tried:

find ./foo*.txt | xargs grep -lv "JOBDONE"  | xargs grep -l "Execution halted"
find ./foo*.txt -exec grep -lv "JOBDONE" {} \;  | xargs grep -l "Execution halted"    

but have been incorrectly (to my understanding) returning

./foo2.txt
./foo3.txt

What is wrong with my understanding of how xargs and exec works with grep and how do I use grep or another portable command to select those logs that have JOBDONE but not Execution halted or vice versa?

mlegge
  • 6,763
  • 3
  • 40
  • 67
  • What you want is an exclusive or (XOR). Check [this answer](http://stackoverflow.com/questions/247167/exclusive-or-in-regular-expression). – m0skit0 Feb 26 '15 at 16:20

1 Answers1

2

Here is an gnu awk (gnu due to multiple characters in RS)

awk -v RS="#-#-#" '/JOBDONE/ && /Execution halted/ {print FILENAME}' foo*
foo3.txt

Setting RS to something that is not in the file, it will thread all lines as one.
Then test if the long line has both string, and if yes, print filename

Jotne
  • 40,548
  • 12
  • 51
  • 55