LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2008, 10:52 PM   #1
sir_com
Member
 
Registered: Feb 2008
Posts: 31

Rep: Reputation: 15
Viewing specified number of lines


Hi All,

Here is the scenario...

I have a file say "test". File contains 40 lines. I want to view only lines between 20 to 30 (inclusive of 20th line & 30th line).

What command to use to achive the same? Please guide.

Thanks,
sir_com
 
Old 11-16-2008, 11:22 PM   #2
centos82
Member
 
Registered: Sep 2008
Distribution: CentOS
Posts: 75

Rep: Reputation: 16
I have two kind of simple solutions for you.

1. If your file will always be 40 lines long then you can do:

cat filename|tail -20|head -10

2. If you don't know how long your file will be but you know you want lines 20 to 30 you can do

totallines=$(cat filename|wc -l)
cat filename|tail -$( echo $(( totallines - 20 )) )|head -10
 
Old 11-17-2008, 12:27 AM   #3
sir_com
Member
 
Registered: Feb 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks centos82. Thanks a lot buddy. I got it now.
 
  


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
cat: output specific number of lines mikeshn Linux - Software 8 01-24-2017 11:08 AM
number of lines for runlevel 3 RAFAL Linux - General 3 07-24-2008 11:53 AM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 04:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 PM.

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