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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-31-2003, 11:35 AM
|
#1
|
Member
Registered: Feb 2002
Posts: 586
Rep:
|
cat: output specific number of lines
Can cat command output specific number of lines? Example, output lines from 1 to 10 .
|
|
|
12-31-2003, 11:58 AM
|
#2
|
Senior Member
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552
Rep:
|
Try using head:
head -10 /path/to/file
|
|
|
12-31-2003, 12:02 PM
|
#3
|
Member
Registered: Feb 2002
Posts: 586
Original Poster
Rep:
|
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 ?
|
|
|
12-31-2003, 01:15 PM
|
#4
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
head -30 /path/to/file | tail -20
print the last 20 of the first 30 lines ie 10-30
|
|
|
01-24-2017, 10:02 AM
|
#5
|
LQ Newbie
Registered: Jan 2017
Posts: 1
Rep: 
|
I think the solution
Quote:
Originally Posted by mikeshn
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. 
|
|
|
01-24-2017, 10:10 AM
|
#6
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
Originally Posted by mikeshn
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.
|
|
|
01-24-2017, 10:13 AM
|
#7
|
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
|
Quote:
Originally Posted by mikeshn
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.
|
|
|
01-24-2017, 10:22 AM
|
#8
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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
|
|
|
01-24-2017, 12:08 PM
|
#9
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,021
|
Easy with sed
Code:
sed -n '10,30p' input
or
Code:
sed '10,30!d' input
--
This is a joke, isn't it?
Last edited by MadeInGermany; 01-24-2017 at 03:34 PM.
|
|
|
All times are GMT -5. The time now is 04:32 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|