LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cat a range (https://www.linuxquestions.org/questions/linux-general-1/cat-a-range-452156/)

cachemonet 06-06-2006 01:09 PM

cat a range
 
Is it possible to cat ( or whatever else works ) a range of text. For example say I have a 500 line text file and I want to cat lines 200-250 , is this possible without custom scripting?

nadroj 06-06-2006 01:19 PM

sure, check out 'man head'. this will show you the manual page for the 'head' command. head is used to show the first 10 lines of a file, by default. there is an option to specify the first X lines of a file.

haertig 06-06-2006 01:26 PM

To the best of my knowledge, "head" won't allow you to skip forward and start printing on line 200 as in the OP's example. "head" will work for printing the first lines of a file, "tail" for printing the last, but I don't think either will support printing the middle of a file. Maybe I should review the manpages on these two commands to find out for sure!

But I know you can use the "sed" command for this:
Code:

sed -n -e'200,250p' /path/to/your/file

nadroj 06-06-2006 01:29 PM

sorry.. i misread the question. your right haertig. i thought he wanted to see the first 200 or 250 lines, in which case head would have worked. but thats not the case and i was wrong. thanks for clarifying.

cachemonet 06-06-2006 01:34 PM

Thanks guys. sed is exactly what i was looking for. Had I thought about it, I should have come up with it on my own Ayway, thanks.

haertig 06-06-2006 01:43 PM

If you don't know the specific line numbers for the section of the file you want, you can use sed with text strings also:
Code:

$ cat testfile
How
now
brown
cow
---start here---
This is the
text that
we want
---end here---
The
cow
jumped
over
the
moon
$ sed -n -e '/---start here---/,/---end here---/p' testfile
---start here---
This is the
text that
we want
---end here---
$


cachemonet 06-06-2006 10:39 PM

Thanks a lot. It works but I dont understand the options. Man pages state
-e script
-n silent
-p umm i couldnt find that option

I really appreciate the tip I'd just want to understand it. THANKS A LOT

pixellany 06-06-2006 11:29 PM

p = print

The "n" option suppresses printing, and then "p" says print when the stated conditions are matched. "p" is in the man page--look again.

Better, however, is a good tutorial---like this:

http://www.grymoire.com/Unix/Sed.html#uh-8

osor 06-07-2006 12:19 AM

Of course, if you want something more basic than sed, both ed and ex use similar range tokens.

evilbohdran 06-07-2006 11:54 AM

Another way - to show lines 20-25 of a text file:


head -n 25 foobar.txt | tail -n `expr 25 - 20 + 1`

(or replace `expr ...` with the number of lines to show,
in this example "6").


All times are GMT -5. The time now is 11:57 PM.