LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 04: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, 04:55 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

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

Rep: Reputation: 36
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, 05: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, 06:55 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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 57 04-17-2016 07:07 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 07:09 PM
Extracting the line number in Unix Shell? Aziz Programming 2 12-01-2004 12:03 PM
Quick Q: Command to print line number X? KneeLess Linux - Software 1 05-27-2004 08:49 PM

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

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