How to display specific lines?

0194

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

Using sed

To display lines 3 to 4 of a file named example.txt:

sed -n '3,4p' example.txt

Using awk

To display lines 3 to 4:

awk 'NR>=3 && NR<=4' example.txt

Using head and tail

To display lines 3 to 4:

head -n 4 example.txt | tail -n 2

Explanation:

  • sed -n '3,4p': The -n option suppresses automatic printing, and 3,4p tells sed to print lines 3 to 4.
  • awk 'NR>=3 && NR<=4': NR is the current line number, and this command prints lines where the line number is between 3 and 4.
  • head -n 4 | tail -n 2: This first gets the first 4 lines and then pipes that output to tail, which retrieves the last 2 lines from that output.

Choose the method that best fits your needs!

0 Comments

no data
Be the first to share your comment!