![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
|||||
|
Quote:
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 |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|