LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2008, 11:34 AM   #16
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question


I do appreciate this. That does the trick, just for curiosity sake, how do you use grep to get and either or of the 2 strings?
 
Old 05-15-2008, 12:17 PM   #17
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
grep's -e option can be used multiple times to specify more than one pattern which will result in a match, e.g.
Code:
$ cat testfile
one two
two three
three four
five
$ grep -e three -e five testfile
two three
three four
five
You can do a test for lines which match more than one pattern by grepping the output of grep:
Code:
$ cat testfile2
a
ab
abc
bcde
$ grep a testfile2 |grep c
abc
Note that grep will read from standard input if no file name is specified. This is functionally the same as the command above:
Code:
cat testfile2 |grep a |grep c
grep sets the error level to 0 if a math was found, 1 if no match was found, and 2 if there was an error. When several commands together, the last one in the line will determine the error level of the pipeline as a whole, so this works as and:
Code:
$ grep a testfile2 |grep c && echo "we found something" || echo "we did not find anything"
abc
we found something
$ grep a testfile2 |grep f && echo "we found something" || echo "we did not find anything"
we did not find anything
I'd like to clarify the && and || syntax. These look at the value of the error level variable "$?". An example should help clarify what is happening:
Code:
grep ab testfile2 && echo "found ab" || echo "ab not found"
Is the same as doing this:
Code:
grep ab testfile2
if [ $? -eq 0 ]; then
    echo "found ab"
else
    echo "ab not found"
fi
It's not as easy to read if you are not familiar with the syntax, but it's a lot less typing :-)
 
  


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
Sound Trouble + Modem Trouble on Knoppix 4.0 AJones Linux - Software 0 01-05-2007 12:06 PM
Trouble with SCSI, Trouble with Sata humbletech99 Linux - Hardware 0 05-24-2006 04:05 AM
im in trouble rxs160 Linux - Software 9 09-23-2003 10:43 PM
trouble ahead, trouble behind....trouble with mplayer Goonie Linux - Software 3 07-02-2003 02:29 AM
Kernel Trouble (Not actually trouble though) chem1 Linux - General 4 10-01-2002 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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