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

Re: Replacing sed for zsh portability



    Hi Peter :)

 * Peter Stephenson <pws@xxxxxxx> dixit:
> DervishD wrote:
> >     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#=>'}

    This prints all the script *except* the lines I want ;)

    Anyway, can I get rid of the 'S' flag if I know that the
delimiter won't appear in the script code?

    Using the 'read-entire-file' solution, I'm using this expression
right now:

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

    Which gives a segmentation fault! It happens even when doing "zsh
-f", using 4.2.5 (i686-pc-linux-gnu)

    This expression below:
    print -r ${file//(#b)*'#<='(*)'#=>'*/$match}

    gives me the result but with the hashes before the lines, and
uses the '\n' already in the text as separators. A bit prettier,
don't know. And this expression works OK:

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

    Don't know why the second substitution works and the first gave a
sigsegv. I may do a little research if you want me to. I'm not
familiar with the sources, but the fail may be on a memcpy call, at
least is there where it fails using gdb without debugging information
:((

> 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#=>'}

    Fortunately the comment won't never be at the start (the GPL
disclaimer is the first thing in the script just after the bangpath.

> >     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##\#(<=|=>| |)}

    Yes, read my autoreply about my stupidity when writing the
expression. I just missed the second '#'. I noticed when rereading my
own message. Only then the single '#' appeared as a beacon. Sorry O:)

    Thanks for your answer, Peter :) Very good solution. I refrained
from reading the entire file into memory although I am pretty sure
the scripts are small enough.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to...



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