Grep help (match two strings one line)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep help (match two strings one line)
# 1  
Old 02-24-2013
Grep help (match two strings one line)

Hello,

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

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

Last edited by holyearth; 02-24-2013 at 01:42 PM..
# 2  
Old 02-24-2013
Try:
Code:
... | xargs grep -lE 'Date.*800|800.*Date'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-24-2013
Thanks, that's a good solution!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

3. Shell Programming and Scripting

Grep: show only first match per line?

Hi, can I make grep stop after finding the first occurrence of a regex in a line? Given: file with various regex patterns file to be grep'ed Sadly some of the regex patterns cannot be limited any further, so grep -Eiof patterns.txt file.txt (GNU grep 2.20) will give me possibly n hits... (4 Replies)
Discussion started by: stresing
4 Replies

4. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

5. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

6. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

7. UNIX for Dummies Questions & Answers

Greping next line after grep match

Hi All I have a file with tha same line multiple times and its easy to grep out those lines using grep "pattern" filealthough I need to know exactly what the next line after those lines are Can anyone please shed some light on this on how i can simultaneously grep the pattern and the next line... (9 Replies)
Discussion started by: pawannoel
9 Replies

8. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

10. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies
Login or Register to Ask a Question