LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-10-2011, 05:40 PM   #1
krist_m
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Rep: Reputation: 0
grep multiple strings


Hi,

I have a text file with following content.

interface TenGigE0/1/0/0
interface TenGigE0/0/0/0
interface TenGigE0/0/0/1
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
interface TenGigE0/0/0/2
interface TenGigE0/0/0/3
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
interface TenGigE0/0/0/4
interface TenGigE0/0/0/5
interface TenGigE0/0/0/6

What will be the correct Linux syntax(grep, egrep?) to get the following output.

interface TenGigE0/0/0/1
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
interface TenGigE0/0/0/3
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress

Any help will be highly appreciated.

Thanks.
 
Old 01-10-2011, 05:51 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Use -e for multiple expressions.
Code:
grep -e "E0/0/0/1" -e "E0/0/0/3" -e "TEST-SAMPLER" inputfile.txt
You could also put the strings you want to search for in a separate file, one per line, then call it with -f.
Code:
grep -f searchfile.txt inputfile.txt
Finally, consider using the -F option if you only need to search for fixed strings instead of regex patterns.
 
Old 01-11-2011, 01:47 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Looks like you want 'sampler' plus preceding line

grep -B1 sampler <yourfile>
 
1 members found this post helpful.
Old 01-11-2011, 07:25 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ah, I hadn't noticed that possibility.

But there's a small problem with the -ABC options in grep. It puts a -- separator between each contiguous block of text found.
Code:
$ grep -B1 sampler inputfile.txt
interface TenGigE0/0/0/1
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
--
interface TenGigE0/0/0/3
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
And since, for some inexplicable reason, there's no option in grep to turn this off, you have to then pipe the output through another grep instance (or similar), to remove them.
Code:
grep -B1 sampler inputfile.txt | grep -v -e "--"
(Note that the -e is necessary here, even though there's only one expression, because otherwise the leading hyphen will make grep think it's a command option rather than a pattern. grep -v -- "--" will also work.)
 
1 members found this post helpful.
Old 01-11-2011, 11:43 AM   #5
krist_m
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you guys. It worked like a charm.

krist_m@ubuntu:~$ grep -B1 sampler raw-input.txt | grep -v -e "--"
interface TenGigE0/0/0/1
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
interface TenGigE0/0/0/3
flow ipv4 monitor TEST sampler TEST-SAMPLER ingress
 
  


Reply



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
grep multiple strings GEEXTER Linux - General 7 12-06-2013 09:56 PM
Grep -- finding files that contain two separate strings Fliggerty Linux - Newbie 4 12-10-2010 08:40 AM
grep searching for strings with '(apostrophe) macsdev Programming 5 11-11-2010 11:46 PM
How to grep two strings in one line in a file? thomas2004ch Linux - Newbie 5 09-15-2009 04:49 AM
grep two strings Hondro Linux - General 3 09-08-2008 09:55 PM

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

All times are GMT -5. The time now is 02:52 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