LinuxQuestions.org
Review your favorite Linux distribution.
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 03-31-2005, 08:37 AM   #1
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Rep: Reputation: 15
Grep related: checking for This AND That


How can I use grep or any other command to check for String A AND String B in a given block of text that will be passed to grep (or other) using the |

Basically we would be reading a file and would like to check if a certain string A is NOT present in that file AND a certain string B is NOT present in that file.

IF both A & B are NOT present then do some action...

I can do it for 1 string, but not A & B together in 1 line.
 
Old 03-31-2005, 09:01 AM   #2
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
infile=inf.txt
string1=one
string2=two

if [ -z "`grep $string1 $infile``grep $string2 $infile`" ] ; then
        echo "Do the thing"
fi
I have a feeling that there is a more elegant solution...

HTH.
 
Old 03-31-2005, 09:11 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
If you want to do something if both A and B are not on the same line:
Code:
grep 'A' file.txt | grep -q 'B' || echo "Do something"
If you want to do something if both A and B are not present in the entire file:
Code:
FILE="file.txt" ; grep -q 'bbbb' $FILE || grep -q 'aaaa' $FILE || echo "Do something"
 
Old 04-01-2005, 03:41 AM   #4
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Original Poster
Rep: Reputation: 15
thanks!!

thank you very much.
!!
 
  


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 ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
grep and framebuffer errors... related? kodiakmook Linux - Software 2 01-29-2002 07:05 PM

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

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