sedplan-9

How do you remove space from the beginning of a line using Plan 9's sed


Seems like this should just remove at most 2 spaces from the start of each line: cat test.txt | 9 sed 's/^ //g; instead it replaces all spaces from the start of the line. GNU's sed seems to be have as I'd expect here, for comparison, but I'm interested in learning the Plan 9 way.

Note: the 9 sed syntax here is because I'm running it from plan9port.

In more detail:

$ cat test.txt
This
  is
    a test.
Bye
$ cat test.txt | 9 sed 's/^  //g'
This
is
a test.
Bye

I would expect that the output would be more like using GNU sed:

$ cat test.txt | sed 's/^  //g'
This
is
  a test.
Bye

Solution

  • Looks like plan 9s sed is broken as it's treating

    sed 's/^  //g'
    

    as if it was (pseudo-code):

    do
        sed 's/^  //'
    until the sed output is no longer different from the input
    

    or:

    sed 's/^  *//'
    

    or similar instead of the correct interpretation which is the same as if the g wasn't present:

    sed 's/^  //'