Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-16-2008, 10:52 PM   #1
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.

Old 11-16-2008, 11:22 PM   #2
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
Registered: Feb 2008
Posts: 31

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


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
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 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 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
Open Source Consulting | Domain Registration