SED - Numbering lines

December 2016

  • 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

Related :

This document entitled « SED - Numbering lines » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.