Grep issue (match two strings on same line)

0

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

holyearth

Posted 2013-02-24T17:44:06.510

Reputation: 1

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 – 2013-02-24T18:21:49.333

Answers

3

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"

parkydr

Posted 2013-02-24T17:44:06.510

Reputation: 2 074