Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Replacing sed for zsh portability

DervishD wrote:
> #<=
> # This is an example of documentation
> # Here are more lines
> #
> # Yet even more lines here
> ...
> #=>
>     Since I cannot use sed for the entire work unless I complicate
> the script (doing multiple substitutions and the like), I want to get
> rid of the current script and doing all the job in ZSH. It doesn't
> matter if it is very slow, speed here is not an issue.
>     Can this be done in zsh easily or the only way is to use a while
> loop to ignore lines before the delimiter and another to process the
> documentation itself?

A while loop is the obvious way.  You can read in the entire file
(file="$(<file)") and substitute on that.  My example succeeded with this:

print -r ${(S)file//$'\n#<='*$'\n#=>'}

although the following is better since it takes account of the fact
that a comment may be at the start: 

print -r ${(S)file//($'\n'|(#s))\#\<\=*$'\n#=>'}

This requires extended_glob.

>     Another question, related to this. It seems that the X|Y glob
> operator doesn't work left-to-right, but shortest-first. If I do
> this:
>     print ${line#\#(<=|=>| |)}
>     then the 'nothing' at the end is used as the match, and the
> delimiters are not matched.

You're missing the fact that your substitution is of the form ${line#...}.
This is explicitly documented to remove the shortest matching chunk at
the head of the line.  Try adding another "#":

    print ${line##\#(<=|=>| |)}

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.


Messages sorted by: Reverse Date, Date, Thread, Author