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 and shell scripting languages 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 10:26 AM
grep for a variable problem MizzGail UNIX for Dummies Questions & Answers 7 03-01-2006 04:09 PM
setting variable from grep | cut doublejz Shell Programming and Scripting 1 08-24-2005 04:39 PM
grep embedded in a variable Nicol Shell Programming and Scripting 6 04-14-2005 05:44 AM
How to grep a variable? whatisthis Shell Programming and Scripting 2 09-14-2004 07:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
ozvena ozvena is offline
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!
  #2 (permalink)  
Old 05-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Whitespace around the "Socket" in A.lst?
  #3 (permalink)  
Old 05-14-2008
ozvena ozvena is offline
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

  #4 (permalink)  
Old 05-14-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
Why not:


Code:
grep -c -f A.list B.tmp

Regards
  #5 (permalink)  
Old 05-14-2008
ozvena ozvena is offline
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.
  #6 (permalink)  
Old 05-14-2008
Danny.Chouinard Danny.Chouinard is offline
Registered User
  
 

Join Date: May 2008
Posts: 21
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0