LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 09-15-2009, 01:03 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 480

Rep: Reputation: 31
How to grep two strings in one line in a file?


I want to grep in a file to find out which line contains the following two strings:

"Mon Sep 14" and "myWeb".

How can I do that?

I try using grep "Mon Sep 14 && myWeb" but it doesn't do what I want.
 
Old 09-15-2009, 01:32 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by thomas2004ch View Post
I want to grep in a file to find out which line contains the following two strings:

"Mon Sep 14" and "myWeb".

How can I do that?

I try using grep "Mon Sep 14 && myWeb" but it doesn't do what I want.
Do you want to test whether both of two test strings are present at once, or whether either string is present?

Either A or B:

Code:
$ cat datafile | grep -P "\b(first|second)\b"
Both A and B:

Code:
$ cat datafile | grep -P "\bfirst\b" | grep -P "\bsecond\b"
The above examples will print the lines in which their specified targets appear. Remember these tests are line-oriented -- for other kinds of partitions, the test must be arranged differently.

Last edited by lutusp; 09-15-2009 at 02:26 AM. Reason: Corrected typo
 
Old 09-15-2009, 01:55 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
note that grep takes a filename as an arg, so cat is unneeded
Code:
grep -P "\b(first|second)\b" datafile
grep -P "\bfirst\b" datafile | grep -P "\bsecond\b"
 
Old 09-15-2009, 03:54 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 480

Original Poster
Rep: Reputation: 31
Just a concrete example:

I want to grep the lines which contains string "Aug 27" and "meth='POST' file='/mycom/Calculator'" in a file called "myconfig.conf". How can I do that?
 
Old 09-15-2009, 04:42 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by thomas2004ch View Post
Just a concrete example:

I want to grep the lines which contains string "Aug 27" and "meth='POST' file='/mycom/Calculator'" in a file called "myconfig.conf". How can I do that?
Like this:

Code:
cat myconfig.conf | grep -P "Aug 27" | grep -P "meth='POST' file='/mycom/Calculator'"
 
Old 09-15-2009, 04:49 AM   #6
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by chrism01 View Post
note that grep takes a filename as an arg, so cat is unneeded
Code:
grep -P "\b(first|second)\b" datafile
grep -P "\bfirst\b" datafile | grep -P "\bsecond\b"
I'm sure you can imagine why I use "cat" in a case where there may be more than one "grep" call present on a line. This is, after all, a newbie discussion group, where aesthetics and clarity of expression matter at least as much as issues of efficiency.

To decide how much clarity of expression matters to newbies, one need only read the interminable threads about the syntax of "xargs", many of which expire more from exhaustion than resolution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple strings GEEXTER Linux - General 7 12-06-2013 09:56 PM
How to grep more than one line from a file markotitel Linux - Software 10 09-03-2009 06:33 AM
grep two strings Hondro Linux - General 3 09-08-2008 09:55 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM


All times are GMT -5. The time now is 09:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration