The UNIX and Linux Forums  


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

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 03-13-2008
hidachab hidachab is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1
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.
  #2 (permalink)  
Old 03-13-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252

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'

  #3 (permalink)  
Old 03-13-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
Many ways, couple follow:


Code:
#  grep -n fff infile | cut -d: -f1


Code:
awk '$0~"fff"{print NR}' infile

(use nawk or /usr/xpg4/bin/awk on solaris)

or just:


Code:
# awk '/fff/{print NR}' infile

Closed Thread

Bookmarks

Tags
perl, perl regex, regex

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 02:19 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