How to number specific lines?

To number specific lines in a file, you can use the sed command along with a combination of line addressing. Here’s how you can do it:

For example, if you want to number lines 2 to 4 in a file named filename.txt, you can use the following command:

sed '2,4=; 2,4p' filename.txt | nl

In this command:

  • 2,4= tells sed to print the line numbers for lines 2 to 4.
  • 2,4p prints those specific lines.
  • nl is used to format the output with line numbers.

Alternatively, if you want to number specific lines and keep the rest of the file unchanged, you can use awk like this:

awk 'NR==2 {print NR, $0} NR==3 {print NR, $0} NR==4 {print NR, $0}' filename.txt

This command will only print lines 2, 3, and 4 with their corresponding line numbers.

You can adjust the line numbers in the commands above to suit your needs.

0 Comments

no data
Be the first to share your comment!