LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-31-2003, 10:35 AM   #1
mikeshn
Member
 
Registered: Feb 2002
Posts: 586

Rep: Reputation: 30
cat: output specific number of lines


Can cat command output specific number of lines? Example, output lines from 1 to 10 .
 
Old 12-31-2003, 10:58 AM   #2
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
Try using head:
head -10 /path/to/file
 
Old 12-31-2003, 11:02 AM   #3
mikeshn
Member
 
Registered: Feb 2002
Posts: 586

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by stickman
Try using head:
head -10 /path/to/file
Is it possible to output from position 10 to 30 ?
With two parameters ?
 
Old 12-31-2003, 12:15 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
head -30 /path/to/file | tail -20

print the last 20 of the first 30 lines ie 10-30
 
Old 01-24-2017, 09:02 AM   #5
jeschb7
LQ Newbie
 
Registered: Jan 2017
Posts: 1

Rep: Reputation: Disabled
Wink I think the solution

Quote:
Originally Posted by mikeshn View Post
Can cat command output specific number of lines? Example, output lines from 1 to 10 .
Hello, in my case i tried with sed and awk:

#This from line 5 to line 10
awk 'NR >= 5 && NR <= 10' <file-path/file.dat>

#This from line 1 to line 10
awk 'NR >= 1 && NR <= 10' <file-path/Samefile.dat>

Regards.
 
Old 01-24-2017, 09:10 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586
Quote:
Originally Posted by mikeshn View Post
Can cat command output specific number of lines? Example, output lines from 1 to 10 .
As to this question: No. Cat cannot display only certain lines. As mentioned above, you will have to use awk, sed, head, tail or some combination to achieve that end.
 
Old 01-24-2017, 09:13 AM   #7
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,135

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Quote:
Originally Posted by mikeshn View Post
Is it possible to output from position 10 to 30 ?
With two parameters ?
By the above, do you mean those lines, or those columns?
Lines can be selected using a combination of head and tail, columns by using cut, and there are many alternate solutions using things like Perl, awk, python, php, and any of hundreds of other tools.

What have you attempted to apply to the problem? (Other than 'cat'.)

Last edited by wpeckham; 01-24-2017 at 09:36 AM.
 
Old 01-24-2017, 09:22 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,185

Rep: Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586Reputation: 1586
Actually- Purely as an exercise, because using sed is so much easier, but using a script I suppose you could 'technically' say that you are using cat to do so:

Code:
num=1
while read line; do
 if [[ ($num -ge 1) && ($n -le 10)  ]]; then
  echo $line | cat -
 elif [[ $num -gt 10 ]]; then
  break
 fi
 n=$(( $n + 1 ))
done < input
 
Old 01-24-2017, 11:08 AM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 349

Rep: Reputation: 164Reputation: 164
Easy with sed
Code:
sed -n '10,30p' input
or
Code:
sed '10,30!d' input
--
Quote:
| cat -
This is a joke, isn't it?

Last edited by MadeInGermany; 01-24-2017 at 02:34 PM.
 
  


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
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 04:01 PM
Cat input output error zaicheke Slackware 15 12-26-2004 09:08 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM


All times are GMT -5. The time now is 11:34 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration