LinuxQuestions.org
Visit Jeremy's Blog.
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, 11: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, 11: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, 12:02 PM   #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, 01:15 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

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

print the last 20 of the first 30 lines ie 10-30
 
Old 01-24-2017, 10: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, 10:10 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 10:13 AM   #7
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,080

Rep: Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875Reputation: 2875
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 10:36 AM.
 
Old 01-24-2017, 10:22 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 12:08 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,021

Rep: Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293
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 03: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 01:36 AM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 05:01 PM
Cat input output error zaicheke Slackware 15 12-26-2004 10:08 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 11:06 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 03:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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