Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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

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

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....

share|improve this question
    
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

1 Answer 1

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"
share|improve this answer
    
Thanks, that works perfect! –  holyearth Feb 24 '13 at 20:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.