LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-01-2012, 09:17 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Search different strings in a single (same) line


Suppose I've a large text file, with content like this:
Quote:
This is for testing Linux, as it is better then any other operating system.
This is for testing Windows, as it is better then any other operating system.
This is for testing Solaris, as it is better then any other operating system.
This is for testing Mac, as it is better then any other operating system.
This is for testing Linux, as it is better then any other operating system.
.... And so on!
I want to filter and display only those lines which contains all 3 strings (i.e. words) "Solaris", "better" "system". How can I achieve this?

One more thing, egrep '(string1|string2) or grep -E '(string1|string2)' displays all lines that either contains both strings or string1 or string2, but I want only those lines having both strings. Although it can be achieved simply by using grep cmd multiple times for every string, but don't want this. So is there any replacement for this?

Last edited by shivaa; 11-01-2012 at 09:18 AM.
 
Old 11-01-2012, 09:50 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
By default bash uses Basic Regular Expressions (BRE). This means certain regular expression characters lose their special meaning so you'll have to use the backslash escape character to give it special meaning again...

Code:
grep 'Solaris\|better\|system' somefile
To match all words you'll require multiple grep statements because non-matching lines will be filtered out with each subsequent grep.

Code:
grep 'Solaris' somefile | grep 'better' | grep 'system'
More on regex and grep
From the grep (GNU grep) 2.10 man page...
Quote:
grep understands three different versions of regular expression syntax: “basic” (BRE), “extended” (ERE) and “perl" (PRCE). In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful.
Quote:
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
Code:
   Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is
              specified by POSIX.)

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret PATTERN as a Perl regular expression (PCRE, see below).  This is highly experimental and grep  -P
              may warn of unimplemented features.
Read your local grep man page for what options are specifically available for you. All greps are not equal.
 
Old 11-01-2012, 10:09 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Thanks @sag47. But the actual situation is little different. File contains lakhs of entries in it with different patterns. So using grep 3 or 4 or multiplpe times is the only option to get desired output? Or is their any better alternative?
 
Old 11-01-2012, 11:04 AM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by shivaa View Post
Thanks @sag47. But the actual situation is little different. File contains lakhs of entries in it with different patterns. So using grep 3 or 4 or multiplpe times is the only option to get desired output? Or is their any better alternative?
grep operates on the stream unlike programs like sort (which takes in all the input and then presents output). So using grep in this manner does not change performance whether you're using it once or 10 times.

That's why grep can work with "tail -f" for example.
 
1 members found this post helpful.
Old 12-07-2012, 11:47 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Solved with:
Code:
awk '/Solaris/ && /better/ && /system/ {print $0}' filename
 
  


Reply

Tags
awk, grep



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for text strings andy.l Linux - General 1 05-19-2011 05:27 PM
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
fastest way to search for strings niteshadw Linux - Newbie 4 01-30-2010 06:24 AM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration