How to display lines from middle of file?

0169

To display lines from the middle of a file, you can use a combination of the head, tail, and wc commands. Here's how you can do it:

  1. Determine the total number of lines in the file using wc -l:

    total_lines=$(wc -l < filename.txt)
  2. Calculate the starting line and the number of lines you want to display. For example, if you want to display lines 10 to 20:

    start_line=10
    number_of_lines=11  # (20 - 10 + 1)
  3. Use head and tail to extract the desired lines:

    head -n $((start_line + number_of_lines - 1)) filename.txt | tail -n $number_of_lines

Putting it all together, here’s a complete example:

total_lines=$(wc -l < filename.txt)
start_line=10
number_of_lines=11  # Display lines 10 to 20

head -n $((start_line + number_of_lines - 1)) filename.txt | tail -n $number_of_lines

This will display lines 10 through 20 from filename.txt. Adjust start_line and number_of_lines as needed for your specific case.

0 Comments

no data
Be the first to share your comment!