Hacker News new | ask | show | jobs
by jandrese 2167 days ago
Hmm:

% sed -rn 's/^### ?//;T;p' testfile

sed: 1: "s/^### ?//;T;p": invalid command code T

Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.

1 comments

Yeah, "man sed" on my machine says, "This is a GNU extension."

You could do the same thing with awk instead:

    awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'
Well sure, there are tons of ways to do this in other languages. :)

Perl for example was made for problems like this.

   perl -ne 'print if ( s/^### ?// )'
Or

    sed -rn 's/^### ?//p'
Doesn't appear anyone has tried addressing before replacement - ie the simplest sed work-a-like - if you don't mind the leading ### is just:

  sed -n '/^### /p' 
I believe? (equivalent to grep).

Then eg:

  sed -nr '/^### /s/^.{4}(.*)/\1/p'
(or without the redundant addressing, just:)

  sed -nr 's/^### (.*)/\1/p'
You can simplify

    sed -nr 's/^.{4}(.*)/\1/'
to

    sed -nr 's/^.{4}//
And if you use a pattern for the address, you can repeat it in the substitution by using an empty pattern, so

    sed -nr '/^### /s/^.{4}(.*)/\1/p'
is the same as

    sed -nr '/^### /s/^.{4}//p'
is the same as

    sed -nr '/^### /s///p'
at which point I prefer just the substitution:

    sed -nr 's/^### //p'