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?

m81
  • 2,217
  • 5
  • 31
  • 47

1 Answers1

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
Shravan Yadav
  • 1,297
  • 1
  • 14
  • 26
  • 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). – tripleee Jul 30 '15 at 03:37