How to display lines from the middle?

0116

To display lines from the middle of a file in Linux, you can use a combination of commands like sed, awk, or head and tail. Here are some methods:

Using sed

You can specify a range of lines to display using sed. For example, to display lines 10 to 20:

sed -n '10,20p' filename.txt

Using awk

You can also use awk to print a specific range of lines. For example, to display lines 10 to 20:

awk 'NR>=10 && NR<=20' filename.txt

Using head and tail

You can combine head and tail to extract lines from the middle. For example, to display lines 10 to 20:

head -n 20 filename.txt | tail -n 11

This command first gets the first 20 lines and then pipes that output to tail, which retrieves the last 11 lines (lines 10 to 20).

Summary

These methods allow you to extract and display specific lines from the middle of a file in a Linux environment. You can adjust the line numbers according to your needs.

0 Comments

no data
Be the first to share your comment!