sed: Print comment header block using `sed`
If you include help documentation as a comment block at the top of a file, you can use the following sed
to print it.
sed -E '
/^#!/ d # ignore shebang
/^# ?/ { # match first comment prefix
:header # (label for start of main loop)
// { # match comment prefix (reuse regex)
s/// # remove comment prefix (reuse regex)
n # read next line (implicit print)
b header # (next loop!)
}
Q # quit, now first comment block is finished
}
' $0 # process on $0 i.e. the script itself
Published on: 15 Jun 2022