Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-22-2005, 02:38 PM   #1
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Retrieve only a single line from a file

I know the line number that I need from a file, and all I need is that line...
I've tried "cat -n $filename | grep ^$linenumber", but cat -n appears to pad each line before it prints the line number, and it pads it with different numbers of spaces depending on the this is out.

Anyone know how to do this?
Old 06-22-2005, 02:52 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,390

Rep: Reputation: 409Reputation: 409Reputation: 409Reputation: 409Reputation: 409
With sed, to print the 3rd line :
sed -n 3p $filename
Old 06-22-2005, 02:55 PM   #3
Senior Member
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch buster
Posts: 1,621

Rep: Reputation: 98
cat -n $filename | grep ^$linenumber | cut -d ' ' -f 2-
cat  $filename | sed -e $linenumber/P
second one might not be correct, refer "man sed", it's been long since I used sed last time.
Old 06-22-2005, 03:02 PM   #4
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
*bows before keefaz*

My day just got a lot happier :-)

I'll have to look up what it is that "cut -d ' ' -f 2-" is doing...I'm not entirely clear on it.

Last edited by rose_bud4201; 06-22-2005 at 03:05 PM.
Old 06-22-2005, 06:26 PM   #5
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
this looks relative to what you are asking:

Personally to extract the 50th line of a file I would use this:
head -50 <filename> | tail -1
but there are faster ways to do that.

Last edited by perfect_circle; 06-22-2005 at 06:29 PM.


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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM
reading a single line of a text file davi_cabral Linux - Software 1 10-29-2003 12:24 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 AM.

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