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 > 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
extract a line from a file using the line number grandtheftander Shell Programming and Scripting 7 01-22-2009 10:24 AM
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 01:39 PM
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 10:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2005
mskarica mskarica is offline
Registered User
  
 

Join Date: Jan 2005
Location: Tallahassee, FL
Posts: 19
Using grep to extract line number

I'm trying to use grep to get the line number only. This is the command I'm using:
grep -n "Content-Disposition: attachment" mbox

The output I get is:
45:Content-Disposition: attachment; filename="test.txt"

So now I just want to get the line number (45) from this output.

Can someone help me with this? Thanks
  #2 (permalink)  
Old 03-19-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
Quote:
Originally Posted by mskarica
I'm trying to use grep to get the line number only. This is the command I'm using:
grep -n "Content-Disposition: attachment" mbox

The output I get is:
45:Content-Disposition: attachment; filename="test.txt"

So now I just want to get the line number (45) from this output.

Can someone help me with this? Thanks
Code:

grep -n "Content-Disposition: attachment" mbox | sed -n 's/^\([0-9]*\)[:].*/\1/p'
  #3 (permalink)  
Old 03-19-2005
mskarica mskarica is offline
Registered User
  
 

Join Date: Jan 2005
Location: Tallahassee, FL
Posts: 19
Thanks. That worked exactly how I needed it to. If you don't mind, can you explain what this means for me so I know in the future:

's/^\([0-9]*\)[:].*/ \ 1/p'

Thanks a lot.
  #4 (permalink)  
Old 03-19-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
Quote:
Originally Posted by mskarica
Thanks. That worked exactly how I needed it to. If you don't mind, can you explain what this means for me so I know in the future:

's/^\([0-9]*\)[:].*/ \ 1/p'

Thanks a lot.
You'll need to read up on regular expressions to really make much use of it, but here it is anyway:

The -n means not to print anything unless it's explicitly requested.

s - substitute
/ - beginning of patter to match
^ - The null character at the start of the line
\(....\) - store this in the pattern buffer
[0-9]* - match any number of occurrences numbers in the range 0-9
[:] - match the ":" character
.* - match any number of any characters (the rest of the line)
/ - end on the match patter and beginning on the replace pattern
\1 - the first entry in the pattern buffer ( what was stored with \(...\) )
/ - end of the replace pattern
p - print
  #5 (permalink)  
Old 03-19-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
you can also try this ...

Code:
grep -n "Content-Disposition: attachment" mbox | awk -F: '{print $1}'
  #6 (permalink)  
Old 03-19-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Or even more concisely....
Code:
sed -n '/Content-Disposition: attachment/=' file_name
Cheers
ZB
  #7 (permalink)  
Old 03-19-2005
mskarica mskarica is offline
Registered User
  
 

Join Date: Jan 2005
Location: Tallahassee, FL
Posts: 19
I tried all 3, and all 3 worked perfectly. The last 2 options are a whole lot easier for me to understand, even though I did read up on regular expressions, and was able to follow some of the first option.

Thanks for your help
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

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 09:37 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