Printing specific lines of a file in terminal

It seems pretty silly, but I didn't find a tool that does this, so I figured I'd just ask to make sure it doesn't exist before trying to code it myself:

Is there an easy way cat

or less

specific lines of the file?

I hope for similar behavior:

# -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 easier to evaluate, but I just don't know of a single tool that does this:

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

      

I think both of these functions can be easily created with a mixture head

, tail

and wc -l

, but maybe there are built-in functions that do this, which I am not aware of?

+3


source to share


1 answer


yes awk and sed can help

for lines 5-10

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

      



for lines 10 to the last line

awk 'NR>9' file.txt
sed -n '10,$p' file.txt

      

+7


source







All Articles