LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 10: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, 10:50 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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, 11:03 AM   #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



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 07:32 AM
extracting line number s_shenbaga Linux - Newbie 1 10-21-2005 05:52 AM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 06:18 PM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 06:09 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration