Here I have some grep command which is not working correctly:

cat file1.txt:
Date: Sun, 24 Feb 2013 8:14:06 -0800
peaches melons
cherry sky cloud
green purple

cat file2.txt:
peaches melons 0800
cherry sky cloud
green purple

Now broken command is:

egrep -lir "apples|melons|cherry" /home/test/* | xargs grep -l "Date" | xargs grep -l "0800"

See first argument: file must contain apples OR melons OR cherry Then, second argument: same file must contain "Date" and "0800" ON SAME LINE

So file1.txt should match but not file2.txt - right now both match

Thanks for the help - I think I need grep with regexp to match "Date:[any][any][any]0800" type command to catch "Date" and "0800" on same line....

  • Rather than giving us this very long but broken command, leaving us to guess what it does and how that differs from what you want, could you give us instead an example or two showing sample input and desired output? – Nicole Hamilton Feb 24 '13 at 18:21

I think this is want you mean. Date with zero or more characters followed by 0800

egrep -lir "apples|melons|cherry" file*.txt | xargs grep -l  "Date.*0800"

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.