LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-22-2005, 03:38 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

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 file...so this is out.

Anyone know how to do this?
 
Old 06-22-2005, 03:52 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

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

Rep: Reputation: 84
Code:
cat -n $filename | grep ^$linenumber | cut -d ' ' -f 2-
Code:
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, 04:02 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

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 04:05 PM.
 
Old 06-22-2005, 07:26 PM   #5
perfect_circle
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:
http://www.linuxquestions.org/questi...hreadid=302951


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

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


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


All times are GMT -5. The time now is 08:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration