LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-01-2004, 11:36 AM   #1
Aziz
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: Fedora Core 3
Posts: 29

Rep: Reputation: 15
Exclamation Extracting the line number in Unix Shell?


Lets say I have a file that contains:

A
B
C
D
F

I want to find out the line number of D lets say, which will come out to 4 and 5 for F...

How would I do that..

I tried to do it with awk and didnt work, because the pattern must be static..

Thnx alot people,
 
Old 12-01-2004, 11:50 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
grep -n "^D" filename
The -n tells grep to return the line number the pattern was found on. The ^ character says that the D must be on the beginning of the line.
 
Old 12-01-2004, 12:03 PM   #3
Aziz
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: Fedora Core 3
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by crabboy
Code:
grep -n "^D" filename
The -n tells grep to return the line number the pattern was found on. The ^ character says that the D must be on the beginning of the line.
I will try something like

grep -n "D" filename

Since it can be anywhere at the line..

Thanks Alot Bro.. appreciate it..

Peace,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 08:32 AM
extracting line number s_shenbaga Linux - Newbie 1 10-21-2005 06:52 AM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 07:09 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 06:55 PM


All times are GMT -5. The time now is 03:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration