LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-22-2005, 02:38 PM   #1
rose_bud4201
Member
 
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 file...so this is out.

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

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

Rep: Reputation: 108Reputation: 108
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, 03:02 PM   #4
rose_bud4201
Member
 
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
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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 06:29 PM.
 
  


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

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

All times are GMT -5. The time now is 09:59 AM.

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