LinuxQuestions.org
Review your favorite Linux distribution.
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 12-12-2006, 03:12 PM   #1
ignignokt
LQ Newbie
 
Registered: Sep 2006
Posts: 19

Rep: Reputation: 0
SH Prg: Checking if a string exists in a file


I want to check in a file for a particular string and if it is true, do one thing and if not do another.

i have the following:

Code:
if grep -q "Import completed successfully" /opt/ar/bin/error.log
then
     ....
else 
     ....
fi
Doesnt seem to work. Any help is greatly appreciated.
 
Old 12-12-2006, 03:24 PM   #2
asommer
Member
 
Registered: Mar 2003
Location: North Carolina
Distribution: Gentoo
Posts: 168

Rep: Reputation: 30
I think this should work:

String=$(grep -q "Import completed successfully" /opt/var/bin/error.log)

if [ $String -eq "Import completed successfully" ]
then
....
else
....
fi

I found this on the Advanced Bash Scripting Guide.
 
Old 12-12-2006, 03:51 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
I have an option:
Code:
found=`grep -m 1 "Import completed successfully" /opt/ar/bin/error.log`
if [ $found ]; then
  # string was found
else
  # string was not found
Works for an arbitrary number of matches, and runs in an average O(n/2) time, thanks to the -m option to grep.
 
Old 12-12-2006, 03:54 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 732

Rep: Reputation: 75
Hi, ignignokt.

I think your basic procedure is correct. You can test these things by creating small cases. For example:
Code:
#!/bin/sh

# @(#) s1       Demonstrate match in an if.

string=${1-"two"}

cat >data1 <<EOF
one
two
three
one hundred two
ball two, strike one
nothing
EOF

echo
if grep -q "$string" data1
then
        echo " Success, string :$string: was found."
else
        echo " Sorry, :$string: was not found."
fi

exit 0
Which, when run, produces:
Code:
% ./s1

 Success, string :two: was found.
And fails with:
Code:
% ./s1 five

 Sorry, :five: was not found.
Best wishes ... cheers, makyo
 
Old 12-12-2006, 03:55 PM   #5
asommer
Member
 
Registered: Mar 2003
Location: North Carolina
Distribution: Gentoo
Posts: 168

Rep: Reputation: 30
heh...that's definitely better than my idea. I'm glad I was on the right track though.
 
Old 12-12-2006, 05:50 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I think the OPs example should work, no? Works for me like this:
Code:
$ cat test.dat
here's my test file
it contains apples, bananas, J. R. Bob Dobbs, and a saucer of milk.
Have fun out there, 1998 will come soon!
$ if grep -q "J. R. Bob Dobbs" test.dat; then
>   echo 'yes, HE is here!  Praise Bob!'
> else
>   echo 'I do not see him.'
> fi
yes, HE is here!  Praise Bob!
 
Old 12-14-2006, 01:31 PM   #7
ignignokt
LQ Newbie
 
Registered: Sep 2006
Posts: 19

Original Poster
Rep: Reputation: 0
thanks

Thanks for all the help with this. I finally used the following with great results:

Code:
if grep "Import completed successfully" /opt/ar/bin/error.log
then
....
else
....
fi
 
  


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
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
Checking if file exists Haiyadragon Programming 2 04-26-2004 10:07 AM
checking if a file exists veilig Programming 6 02-01-2004 04:16 AM
Checking if a file exists in C? tristan_vdv Programming 3 05-24-2002 10:06 PM

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

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