SED - Numbering lines

January 2017

  • Numbering lines (same as "cat -n file.txt")
    • sed = file.txt
  • The problem is that the display is effected on 2 lines. To set this as default:
    • sed = file.txt | sed 'N;s/\n/\t/'
  • Number the lines but display only the numbers of non-blank lines.
    • sed '/./=' file.txt | sed '/./N;s/\n/ /'
  • Counting the number of lines (same as "wc -l file.txt | awk '{ print $1 }'")
    • sed -n '$=' file.txt
  • Defining the line number containing a reason
    • sed -n '/Start/=' file.txt
  • The setting= "accepts only one address, to define the numbers of lines between 2 reasons, use the following syntax
    • sed -n '/start,/Fin/{=;d;}' file.txt


Published by deri58. Latest update on June 6, 2010 at 10:17 AM by deri58.
This document, titled "SED - Numbering lines," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (