Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 07-10-2003, 06:27 PM   #1
LQ Newbie
Registered: Nov 2002
Posts: 15

Rep: Reputation: 0
grep multiple strings

This may be a simple question for some of ya.

I would like to grep with multiple strings.... for example:

netstat -anp | grep 45632 | grep 23000 | grep 1100

I know the above command doesn't work, but I'm looking for something similar. I would like to search netstat for multiple greps. I would like the command to return all lines that have 45632, all lines that have 23000, and all lines that have 1100.

Any clue?
Old 07-10-2003, 06:39 PM   #2
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49

I haven't had to grep multiple strings like that, but perhaps grepping the first, routing it to a file, grepping the second, appending the file, and grepping the third, appending to the same file. Then pop open your "results" (or whatever) file and you'll have all your responses. May not be pretty, but at least it's a start, neh?
Old 07-10-2003, 07:42 PM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
egrep my friend... egrep

try this:
netstat -anp | egrep '(45632)|(23000)|(1100)'

regular expressions kick much arse.
Old 07-10-2003, 08:00 PM   #4
LQ Newbie
Registered: Nov 2002
Posts: 15

Original Poster
Rep: Reputation: 0

egrep works great!
Old 07-10-2003, 08:15 PM   #5
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It turns out you can use plain old grep too... After some experimenting:

netstat -anp | grep '45632\|23000\|1100'

That should do the same thing. So the question is:

Which do you like more: backslashes or parentheses?
1 members found this post helpful.
Old 03-27-2004, 11:34 PM   #6
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15

Hi Dark_Helmet,

it seems like you are a expert in grep...can you help me on my thread?..thanks
Old 07-23-2012, 01:23 AM   #7
LQ Newbie
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
suppress multiple lines in a file

Hi Dark_helmet,

i have two lines to supress in one file, how can i do it using "grep -v" command can u please help me in this

Old 12-06-2013, 09:56 PM   #8
LQ Newbie
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
Thumbs up Nice .thank u:)

i find it very useful. Thank you Dark_Helmet \ -this is used to suppress the | and then it is used as pipe operator in grep


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 06:07 PM
Search and Replace with multiple-line strings Programming 4 08-21-2005 02:32 PM
Problem matching strings with grep/egrep Seb74 Linux - Newbie 5 05-26-2005 01:40 PM
Using grep to change multiple files zwyrbla Linux - General 5 01-11-2005 02:14 PM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 10:13 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:10 PM.

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