3 ms·
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 w
by jandrese 6y 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.
- adrianmonk 6y agoYeah, "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; } }'
- bewuethr 6y agoOr sed -rn 's/^### ?//p'
- e12e 6y agoDoesn'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'
- bewuethr 6y agoYou 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'
- jandrese 6y agoWell 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/^### ?// )'
- deleted 6y ago[deleted]