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 02-01-2013, 04:39 AM   #1
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 18

Rep: Reputation: Disabled
check Negative egrep condition in an if condition


Hi
I need to check a negative egrep condition. How is it possible?
Like if (!(cat file | egrep -e 'x|y|z))
 
Old 02-01-2013, 05:25 AM   #2
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Quote:
Originally Posted by novicunix View Post
Hi
I need to check a negative egrep condition. How is it possible?
Like if (!(cat file | egrep -e 'x|y|z))
Hi Novicunix,
Are you looking like this?
You can use -v option to negate any condition.

Code:
cat file | egrep -v 'x|y|z'
 
Old 02-01-2013, 06:04 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So for the both of you, I suggest having a look at the following link when you have some time: http://partmaps.org/era/unix/award.html

As for the if, you have 2 options (one has been suggested but ill formatted):
Code:
if grep -Ev 'x|y|z' file

# or

if ! grep -E 'x|y|z' file
egrep and other derivatives of grep have been deprecated so good to get in the habit of not using them
 
Old 02-01-2013, 10:26 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
For quite a while now, egrep from the 'official' sources is just a link to grep -some distros use a wrapper to grep instead. fgrep is also officially deprecated, but can still be built as a separate binary -which I do because I want fgrep to really be faster than grep -not slower(because of a wrapper or link).
 
Old 02-01-2013, 02:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by grail View Post
As for the if, you have 2 options (one has been suggested but ill formatted):
Code:
if grep -Ev 'x|y|z' file

# or

if ! grep -E 'x|y|z' file
Those options are not equivalent: the first checks whether file has any line not matching x, y, or z; the second checks that there is no line matching x, y, or z. Also, probably the -q option is wanted here.


@gnashely: if you use a hard link instead of symlink I believe you would get the same speed benefits as a separate binary, plus extra savings in space. EDIT: Actually, I was assuming they build to the same binary that checks argv[0], but probably the separate fgrep actually has the regex stuff removed, so maybe what I said doesn't apply.

Last edited by ntubski; 02-01-2013 at 03:05 PM. Reason: second thoughts
 
Old 02-02-2013, 12:52 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
My bad ... ntubski is quite correct I will say however that the OP merely states they want the negative, so I guess it is up to them which most suits their version of that statement
 
  


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
[SOLVED] Check for condition on remote machine before running command? soupmagnet Linux - Newbie 2 01-03-2013 10:31 AM
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
[SOLVED] remort loging, check for a file existince and display a message (if condition) procfs Linux - Newbie 5 05-31-2010 02:32 AM
SCSI Error: (0:4:0) Status=02h (CHECK CONDITION) tss23 Linux - Newbie 1 04-10-2008 08:07 AM
abnormal behavious in comparison check in 'if' condition. indiancosmonaut Programming 10 04-03-2008 12:10 AM

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

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