LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   cat: output specific number of lines (https://www.linuxquestions.org/questions/linux-software-2/cat-output-specific-number-of-lines-130360/)

mikeshn 12-31-2003 10:35 AM

cat: output specific number of lines
 
Can cat command output specific number of lines? Example, output lines from 1 to 10 .

stickman 12-31-2003 10:58 AM

Try using head:
head -10 /path/to/file

mikeshn 12-31-2003 11:02 AM

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 ?

kev82 12-31-2003 12:15 PM

head -30 /path/to/file | tail -20

print the last 20 of the first 30 lines ie 10-30

jeschb7 01-24-2017 09:02 AM

I think the solution
 
Quote:

Originally Posted by mikeshn (Post 676031)
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.:)

szboardstretcher 01-24-2017 09:10 AM

Quote:

Originally Posted by mikeshn (Post 676031)
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.

wpeckham 01-24-2017 09:13 AM

Quote:

Originally Posted by mikeshn (Post 676076)
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'.)

szboardstretcher 01-24-2017 09:22 AM

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


MadeInGermany 01-24-2017 11:08 AM

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?


All times are GMT -5. The time now is 05:54 PM.