The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
after grep i want to redirect it to some variable mail2sant Shell Programming and Scripting 2 04-07-2008 06:26 AM
grep for a variable problem MizzGail UNIX for Dummies Questions & Answers 7 03-01-2006 12:09 PM
setting variable from grep | cut doublejz Shell Programming and Scripting 1 08-24-2005 12:39 PM
grep embedded in a variable Nicol Shell Programming and Scripting 6 04-14-2005 01:44 AM
How to grep a variable? whatisthis Shell Programming and Scripting 2 09-14-2004 03:26 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Jun 2005
Posts: 6
grep with variable

Hi, I can't get this script to work (returns 0, should return 3):

Code:
$ cat A.lst | \
while read LINE
do
        echo "$LINE"
        grep -c "$LINE" B.tmp
done> > > > >
Socket
0
$
but in contrast this one works fine (returns 3 as expected):

Code:
$  LINE=Socket
$ grep -c $LINE B.tmp
3
$

Note: A.lst contains one line with a string: Socket
B.tmp contains 3 lines with strings: Socket

Thank you!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,501
Whitespace around the "Socket" in A.lst?
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Jun 2005
Posts: 6
No, there is not. Even if there is it should work as Socket has spaces around in B.tmp file:


Code:
$ LINE=" Socket "
$ grep -c $LINE B.tmp
$ 3
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 7
Check with od.

Hi.

Try od -c A.lst , I'm also of the opinion that there's something funky in it.

Also, in your last code example, you didn't replicate the quotes around the grep pattern. Those make a large difference.
Reply With Quote
  #5 (permalink)  
Old 05-14-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,487
Why not:

Code:
grep -c -f A.list B.tmp
Regards
Reply With Quote
  #6 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Jun 2005
Posts: 6
Thanks for your help, it works after I did this:

Code:
$  od -c A.lst
0000000   S   o   c   k   e   t  \r  \n
0000010
$ dos2unix A.lst A.lst
$  od -c A.lst
0000000   S   o   c   k   e   t  \n
0000007
The file was created under Win OS.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0