LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 05-03-2011, 09:42 PM   #1
coolnfunky
LQ Newbie
 
Registered: May 2011
Posts: 2

Rep: Reputation: 0
grep fails in shell script when string is not found


Hello

I am having trouble using grep command. I want to search for each line in first file in second file and if they are present, write to file called successfile else to failfile. Below is the code

what is happening is, when the first failure occurs (when a line in first file is not present in second file), the script is killed.

Can anyone please tell me what I am doing wrong?

Code:
        while read line
        do
                temp=""
                if [ "$line" != "" ]
                then
                        grep $line $pwdir/$file
                        if [ "$?" == "0" ]
                        then
                                successcount=$successcount+1
                                echo $line>>$successfile
                        else
                                echo "fail >>>>>>> $line"
                                failcount=$failcount+1
                                echo $line>>$failfile
                        fi
                fi
        done < "$kwd_file"
 
Old 05-03-2011, 11:00 PM   #2
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
I think (!) you want:
a) grep $line $pwdir/$file
use quotes around $line, else you run into trouble if it read reads more than one string in a line.

b) successcount=$successcount+1
use
Code:
((successcount=$successcount))
to get an integer value.

c) echo $line>>successfile
put spaces around the redirection:
Code:
echo $line >> successfile
As far i see that should work.

You can also consider to do it like this:
Code:
if grep "$line" $pwdir/$file
instead of:
grep $line $pwdir/$file
if [ "$?" == "0" ]
grep gives an exit code, either success or failure. "if" uses it.
 
Old 05-03-2011, 11:01 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Well looking at your code I am guessing the first file has blank lines?
You create a variable temp that is never used.

To answer your questions though, use you grep in the if:
Code:
if grep -q $line $pwdir/$file
-q will stop output being sent to the screen for each search which I presume would not be required.
 
Old 05-04-2011, 12:50 AM   #4
coolnfunky
LQ Newbie
 
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the quick responses. I have incorporated all of your suggestions. The script is working as expected. Thanks much!!
 
Old 05-04-2011, 10:02 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
I would add that you can use normal increment (like C) in bash as well:
Code:
(( successcount++ ))
 
  


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
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Shell script with grep MicahCarrick Programming 4 08-15-2006 01:08 PM
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 08:47 PM
use grep and execute a command if string is found plisken Linux - General 6 07-28-2005 11:06 AM
grep in shell script fails on redhat 3.0 rlangsto Linux - General 4 03-06-2005 07:42 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration