My self modifying program (script file) strips the comments to cut the source down from over 500 kB to itself: half the size with just the programming instructions. Reason: to shorten the startup for 0.8 seconds. When the new source file is found, the stripdown takes 0.1 sec.
I have an area after the program with notes, so I mark it with #d- and that is the end of the program.
awk '{
if ( $1 !~ /^#/ || $1 ~ /^#!/ )
{
if ( match($0,/[\t ]#/) ) { print substr($0, 0, RSTART) }
else if ( NF ) { print }
}
else if ( /^#d-/ ) { exit }
}' source.sh > strip.sh
The above works flawlessly in a correctly written script (not for the question) as asdf="5"; echo "$asdf"#test
demonstrates we need a space before the #, possibly some other markers apply, too. Limitation is a free character # in something like echo line or similar.
This solution leaves 154 characters # in my program which are used in the programming context, leaving even the ones in the above command (as it is a self modifying program, it parses the above lines, too).
if ( $1 !~ /^#/ || $1 ~ /^#!/ )
finds the lines not starting with # or #! (meaning: skip the lines starting with # or #!)...
if ( match($0,/[\t ]#/) )
... and check if they contain an inline comment: a # preceded by the tab or the space (here we could add markers).
{ print substr($0, 0, RSTART) }
when an inline comment is found, the above cuts the line dropping out the comment and writing out the line before the comment
else if ( NF ) { print }
simply prints out lines which are not empty (or: skip over empty lines)
else if ( /^#d-/ ) { exit }
can be omitted completely, it will finish stripping when the #d- is found at the beginning of a line.
The above is readable and complete version. It could be shortened if you want bare bones version to paste into the command line:
awk '/^[^#]/||/^#!/{if(match($0,/[\t ]#/))print substr($0,0,RSTART);else print}' source.sh > strip.sh
awk -F\# '$1!="" { print $1 ;} '
.echo '#' # output a #
be handled?