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 01-04-2005, 03:50 PM   #1
rheza
LQ Newbie
 
Registered: Apr 2003
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
Print line number X of a file (in shell)


I didn't think I need it a while ago so I excuse me for another topic.

Is there a way to print line number X of a certain file.

For example:

file "file" has 3 lines:

hello
abc
hi

Is there a simple command to print say, the second line of file "file":

abc
 
Old 01-04-2005, 03:55 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
If nothing else you could just do:
Code:
head -n $num $file | tail -n 1
 
Old 01-04-2005, 03:59 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Assume you want to print just line 29 ---
this is one way to do it:
Code:
head -29 myfile | tail -1
 
Old 01-04-2005, 04:03 PM   #4
rheza
LQ Newbie
 
Registered: Apr 2003
Distribution: Slackware
Posts: 21

Original Poster
Rep: Reputation: 15
Thanks. It's an odd way, but I suppose this is how it's usually/(supposed to be) done. XD
 
Old 01-04-2005, 05:55 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
There's also sed:
Code:
sed -n 17p yourfile.txt
 
  


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 shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
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
Extracting the line number in Unix Shell? Aziz Programming 2 12-01-2004 11:03 AM
Quick Q: Command to print line number X? KneeLess Linux - Software 1 05-27-2004 07:49 PM


All times are GMT -5. The time now is 12:25 AM.

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