The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-06-2008 01:14 AM
Grep Aejaz UNIX for Advanced & Expert Users 3 04-30-2008 07:10 AM
grep dineshr85 Shell Programming and Scripting 1 10-10-2007 04:52 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 12:20 AM

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-24-2007
mz043 mz043 is offline
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!.
  #2 (permalink)  
Old 05-24-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
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 ?
  #3 (permalink)  
Old 05-24-2007
mz043 mz043 is offline
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
  #4 (permalink)  
Old 05-25-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
pointer !

Code:
cat a
1 name
2 name
3 name
.
n name
Code:
i=1
sed -n "/$i/p" a | sed 's/ .*$//'
  #5 (permalink)  
Old 05-25-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,514
Code:
awk '/1/{ 
           for (i=1;i<=NF;i++){
               if ( $i == "1"){
                    print 
               }
           } }  ' "file"
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 01:27 AM.


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