LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-12-2012, 03:21 PM   #16
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373

Quote:
Originally Posted by 2armz View Post
hmm I might not be on the same page as you, i kinda understand what you are asking, but I'm new to this as well, so if i've thrown you off track sorry
Hey, no problem.
I was really trying to ask brian00 how this was supposed to work so that I can understand it and he can double-check it is the right thing.
Personally I'd take away the '$' and replace 'ls' with 'cat' and try that.
 
Old 09-12-2012, 03:30 PM   #17
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
OK, I just tried what I suggested and
Code:
#!/bin/bash
if [[ -n `cat log/output.tst | grep "MYDB"` ]] ; then
echo "found it!"
fi
results in
Code:
$ ./test.sh 
found it!
If there is a match and nothing if there is not.
So to cheat you could echo whatever you wanted.
Of course there will be a way of using grep and regular expressions to do this properly.
The above is for illustrative purposes only to explain the logic I meant.
 
Old 09-12-2012, 04:13 PM   #18
brian00
Member
 
Registered: Aug 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks so much to everyone, specially 273 b/c it worked now.

one more question:

what if I want to add another string, basically, my condition is if both string exit then execute aything below it (both string has to exist)
Code:
#!/bin/bash
if [[ -n `cat log/output.tst | grep "MYDB" | grep "DUMMY"` ]] ; then
echo "found it!"
fi
test the above code and it doesnt' work.
 
Old 09-12-2012, 04:27 PM   #19
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
This works but, again, it's not ideal and someone with a knowledge of scripting ought to be able to point out a correct solution:
Code:
#!/bin/bash
if [[ -n `cat log/output.tst | grep "MYDB"` ]] && [[ -n `cat log/output.tst | grep "DUMMY"` ]] ; then
echo "found it!"
fi
The logic of this is that you're using cat to pipe the file to grep, if you then just add another pipe to grep again as you did it will be working on the line returned by grep and not the whole file. I added a logical and so that both of the expressions need to be not null to enter the if statement.
Remember that the pipe | is just passing the result of the previous action not the whole file. This was my point about passing the result of ls to grep but I'm not familiar enough with the logic of bash to know whether there was an exception there somewhere.

Last edited by 273; 09-12-2012 at 04:29 PM.
 
Old 09-12-2012, 08:43 PM   #20
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
grep MYDB$ <filename here> |grep DUMMY
Matches MYDB at end of rec and 'DUMMY' must exist anywhere in same rec
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to build a script that will search for a specific word in a log file kimbo8 Programming 1 03-27-2012 05:17 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
search file contains word prospekrisal Linux - Newbie 5 07-20-2006 09:24 PM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM

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

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