LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-19-2009, 09:35 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 281

Rep: Reputation: 32
Printing specific lines of a file using script.


Hi,
I have text file with say 1000 lines.
And I want to display lines numbered 100, 203 and 297 using a script/command.
How can I do this ?

I can print a particular line using:
sed -n '100p' file1 (where file1 is input file).

With Thanks,
Barun Parichha
 
Old 05-19-2009, 09:47 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Just print other lines adding sed commands separated by semi-colon:
Code:
sed -n '100p;203p;297p' file1
 
Old 05-19-2009, 09:47 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
sed -n -e '100p' -e '203p -e '297p' filename

Beaten again!!!! colucix was faster and had the better answer........I am humbled.....

Last edited by pixellany; 05-19-2009 at 09:51 AM.
 
Old 05-19-2009, 09:50 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
if you have Python
Code:
#!/usr/bin/env python
for n,line in enumerate(open("file")):
   if n+t1>299: break
   if n+1 in [100,203,297]:
       print line.strip()
for big files, better to quit iterating the file after 297th record is reached.
Code:
awk 'NR==100||NR==203||NR==297{print}NR>300{exit}'  file

Last edited by ghostdog74; 05-19-2009 at 10:01 AM.
 
Old 05-19-2009, 09:58 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
using awk
Code:
 awk 'NR==100||NR==203||NR==297' file1

Last edited by colucix; 05-20-2009 at 12:31 AM.
 
Old 05-19-2009, 10:53 PM   #6
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 281

Original Poster
Rep: Reputation: 32
Hi colucix,
you have missed the file name in your command.

Barun Parichha
 
Old 05-20-2009, 12:31 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by barunparichha View Post
Hi colucix,
you have missed the file name in your command.

Barun Parichha
Right. Thanks. I've corrected my post.
 
  


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
how to retrieve some specific set of lines from a file and store it in a char buffer. vigneshinbox Programming 3 04-02-2009 01:16 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-24-2008 11:04 PM
Bash script question (sending output to specific lines) ptcl Linux - Software 9 11-21-2006 03:53 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM


All times are GMT -5. The time now is 02:45 PM.

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