Latest LQ Deal: Latest LQ Deals
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 01-04-2005, 03:50 PM   #1
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:


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

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

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

Rep: Reputation: 36
Assume you want to print just line 29 ---
this is one way to do it:
head -29 myfile | tail -1
Old 01-04-2005, 04:03 PM   #4
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
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

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


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 57 04-17-2016 06:07 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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 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