The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
grep unix.com with google



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 !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-23-2009
Registered User
 

Join Date: Nov 2007
Posts: 108
Line number of an occurrence using grep

Hi All,
is there a way to extract the line number of an occurrence using grep?
I know that with the -n option it prints out the line number as well.
I would like to assign the line number to a variable.
Thanks,
Sarah
  #2 (permalink)  
Old 11-23-2009
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 6,006

Code:
grep -n 'pattern ' inputfile | read number_var junk
echo $number_var

One way of many. This assumes the pattern occurs only one time in inputfile
  #3 (permalink)  
Old 11-23-2009
Registered User
 

Join Date: Nov 2009
Posts: 12

Code:
file="..."
pattern="..."
a=`cat -n $file | grep $pattern | awk '{print $1}'`

You have in $a the list of line numbers that match the pattern
  #4 (permalink)  
Old 11-23-2009
Registered User
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 80
var=`grep -n pattern inputfile | cut -d : -f 1`
  #5 (permalink)  
Old 11-23-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,259

Code:
var=$(grep -n 'pattern' infile)
echo ${var%%:*}


Code:
var=$(awk '/pattern/{print NR}' infile)


Code:
var=$(sed -n '/pattern/=' infile)


Last edited by Scrutinizer; 11-23-2009 at 01:45 PM..
  #6 (permalink)  
Old 11-24-2009
Registered User
 

Join Date: Nov 2007
Posts: 108
thank you all, how do I access the first value of the variable in case of more than one occurrence?
I tried var[0]...but with no success

---------- Post updated at 05:37 AM ---------- Previous update was at 04:14 AM ----------

Actually I am interested only in the first.


Code:
grep -n 'pattern ' inputfile | read number_var junk
echo $number_var

doesn't work.
Other methods work but I don't know how to access the first value in the returned variable.

I solved with


Code:
ISTHERE=`grep -n total tmp.tmp3 | head -1 | cut -d : -f 1`

I'm smart too!

Last edited by f_o_555; 11-24-2009 at 09:14 AM..
Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using grep to extract line number mskarica Shell Programming and Scripting 8 06-26-2008 03:47 AM
Grep a number from a line in ksh rodluo Shell Programming and Scripting 3 12-09-2007 09:31 AM
grep the string with the line number salaathi Shell Programming and Scripting 4 12-04-2007 11:42 AM
Get Filename and Line Number using grep ceemh3 UNIX for Dummies Questions & Answers 4 09-17-2007 11:35 AM
is there any why to get the number of line in grep result ? umen UNIX for Dummies Questions & Answers 1 03-16-2006 09:56 AM



All times are GMT -4. The time now is 02:24 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