manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: cat -b,  Next: wc -c,  Prev: cat -n,  Up: Examples

7.12 Numbering Non-blank Lines
==============================

Emulating ‘cat -b’ is almost the same as ‘cat -n’--we only have to
select which lines are to be numbered and which are not.

   The part that is common to this script and the previous one is not
commented to show how important it is to comment ‘sed’ scripts
properly...

     #!/usr/bin/sed -nf

     /^$/ {
       p
       b
     }

     # Same as cat -n from now
     x
     /^$/ s/^.*$/1/
     G
     h
     s/^/      /
     s/^ *\(......\)\n/\1  /p
     x
     s/\n.*$//
     /^9*$/ s/^/0/
     s/.9*$/x&/
     h
     s/^.*x//
     y/0123456789/1234567890/
     x
     s/x.*$//
     G
     s/\n//
     h

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.