106k views
3 votes
How do you print the lines between 5 and 10, both inclusive?

1) cat filename | head | tail -6
2) cat filename | head | tail -5
3) cat filename | tail +5 | head

User Caryden
by
7.9k points

1 Answer

4 votes

Final answer:

To print lines 5 to 10 from a file, none of the options provided are correct. The use of 'sed -n '5,10p' filename' or a correct combination of 'head' and 'tail' commands, specifically 'cat filename | head -n 10 | tail -n +6', would achieve this.

Step-by-step explanation:

To print the lines between 5 and 10, both inclusive, from a file using shell commands, the correct command would be:

sed -n '5,10p' filename

However, none of the options provided in the question are correct. The options given manipulate the output of the cat command with a combination of head and tail commands, which can achieve similar results but not with the options given. To explain, the head command, when used without any options, will print the first 10 lines of a file. Using tail with a numeric argument (for example, tail -n +5) will display all lines starting with line number 5. Combining the two can filter a specific range, but neither of the options given combines the commands correctly for this purpose.

To use head and tail to achieve the desired result, the correct command would be:

cat filename | head -n 10 | tail -n +6

This command sequence first uses head -n 10 to get the first 10 lines of the file, then tail -n +6 to print from the 6th line to the end of the input received from the head command, which would be lines 6 through 10, achieving a range of 5-10 lines.

User Bkoodaa
by
8.7k points