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?

1
  • What you want is an exclusive or (XOR). Check this answer. Commented Feb 26, 2015 at 16:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, +1 for the awk, I am assuming I can negate either condition by a preceeding !?
@mkemp6 Yes you can negate it and also add more condition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.