How to display only lines 12-24 of an arbitrary text file?

I have a bunch of text files and I would like to display lines 12-14 by running a bash script for each file.

For one of the files, this works:

tail -14 | head -11

      

But since the other files are of different lengths, I cannot run the same script with them.

What is the command I'm looking for to output lines 12-24 of a text file?

+3


source to share


3 answers


Use sed

with an argument-n



sed -n 12,24p <FILENAME>

      

+5


source


For fun pure Bash (≥4) possibilities:

mapfile -t -s 11 -n 13 lines < file
printf '%s\n' "${lines[@]}"

      



This will skip the first 11 lines (s -s 11

) and read 13 lines (s -n 13

) and store each line in an array field lines

.

+1


source


Usage awk

:

awk '12<= NR && NR <= 24' file

      

B awk

, NR

- line number. The above condition insists on being NR

greater than or equal to 12 and less than or equal to 24. If so, then the line is printed. Otherwise it is not.

More efficient solution

It would be more efficient to stop reading the file after the upper limit is reached. This solution does the following:

awk 'NR>24 {exit;} NR>=12' file

      

0


source







All Articles