![]() |
|
|
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 |
| Adding a columnfrom a specifit line number to a specific line number | Ezy | Shell Programming and Scripting | 2 | 05-12-2008 09:29 AM |
| Appending line number to each line and getting total number of lines | chiru_h | Shell Programming and Scripting | 2 | 03-25-2008 10:19 AM |
| Appending the line number and a seperator to each line of a file ? | pjcwhite | Shell Programming and Scripting | 4 | 03-21-2007 01:29 AM |
| Unix Script with line number at beginning of each line. | mascorro | Shell Programming and Scripting | 5 | 06-19-2006 05:34 PM |
| identifying duplicates line & reporting their line number | stresslog | UNIX for Dummies Questions & Answers | 5 | 04-24-2006 01:43 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Get the line number of an unknown line
I need to get the line number for a line, but I don't know where the file the line will appear.
The line is always constant, but appears in different places. ie abc bcd cde fff aaa bbb ccc ddd and I would need the line number for fff, any help on this would be greatly appreciated. |
|
||||
|
Code:
cat test.txt | perl -ne 'print "$.\n" if $_ eq "fff\n"' There are several variations on this you can use, but this should work. If you prefer a regex, or don't care if the line match is inexact, you can do it this way: Code:
cat test.txt | perl -ne 'print "$.\n" if $_ =~ /fff/' This way, you don't need to specify the \n. Here's a more explicit version: Code:
cat test.txt | perl -ne 'print "$.\n" if $_ =~ /^fff$/' The ^ indicates the beginning of the line, and the $ indicates the end of the line. So this will match it only if "fff" is the entire content of the line. If you want it to be case-insensitive, add "i" after the final backslash: Code:
cat test.txt | perl -ne 'print "$.\n" if $_ =~ /^fff$/i' |
![]() |
| Bookmarks |
| Tags |
| perl, perl regex, regex |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|