9

This seems pretty silly, but I haven't found a tool that does this, so I figured I'd ask just to make sure one doesn't exist before trying to code it up myself:

Is there any easy way to cat or less specific lines of a file?

I'm hoping for behavior like this:

# -s == start && -f == finish
# we want to print lines 5 - 10 of file.txt
cat -s 5 -f 10 file.txt

Even something a little simpler would be appreciated, but I just don't know of any tool that does this:

# print from line 10 to the end of the file
cat -s 10 file.txt

I'm thinking that both of these functionalities could be easily created with a mixture of head, tail, and wc -l, but maybe there are builtins that do this of which I'm unaware?

0

1 Answer 1

15

yes awk and sed can help

for lines 5 to 10

awk 'NR>4&&NR<11' file.txt
sed -n '5,10p' file.txt

for lines 10 to last line

awk 'NR>9' file.txt
sed -n '10,$p' file.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps also note that out of those two, Awk is probably more worthy of further study. sed can be complex, but you'll need the manual anyway to decipher any nontrivial script. Awk is a natural extension to shell script, and will be necessary for many little tasks which aren't quite natural to do in shell script (although modern shells like Bash have filled up many of those holes; but by no means all of them).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.