The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep help flood Shell Programming and Scripting 3 06-05-2008 10:14 PM
Grep Aejaz UNIX for Advanced & Expert Users 3 04-30-2008 04:10 AM
grep dineshr85 Shell Programming and Scripting 1 10-10-2007 01:52 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-04-2007 11:59 PM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-24-2006 09:20 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 05-24-2007
Registered User
 

Join Date: Apr 2007
Posts: 8
help with grep

Hi.

I'm trying to grep a line that starts with a specific number from a file using a loop. The problem is:

grep -w $i filename (e.g. for i=1)

gives me back

line 1
line 10
line 11
...
and so forth. I only want 1 (and not 11, 12, 13. and so forth).

I need a loop that goes up to a large value, so I don't want to restrict the number of characters I'm searching for. I just want to match whole words and not letters within words. I thought the -w flag did that, but it's not. I also tried grep \<1\> to search for just "1" but i get no results back.

Thanks for your help!.
Reply With Quote
Forum Sponsor
  #2  
Old 05-24-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
that should work,

Code:
>cat a
1
2
11
12
1
Code:
>i=1
>grep -w $i a
1
1
Could you please post sample of your input file ?
Reply With Quote
  #3  
Old 05-24-2007
Registered User
 

Join Date: Apr 2007
Posts: 8
hi,
thanks for the reply. the problem is because i'm using cat -n to number the lines in the file and then try to grep the line number. cat -n puts whitespace and i think that's where i 'm going wrong.

the format is

1 name
2 name
3 name
.
n name
thanks
Reply With Quote
  #4  
Old 05-24-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
pointer !

Code:
cat a
1 name
2 name
3 name
.
n name
Code:
i=1
sed -n "/$i/p" a | sed 's/ .*$//'
Reply With Quote
  #5  
Old 05-25-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
awk '/1/{ 
           for (i=1;i<=NF;i++){
               if ( $i == "1"){
                    print 
               }
           } }  ' "file"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:10 PM.


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

Content Relevant URLs by vBSEO 3.2.0