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

Re: line continuation with sed




On 2022-10-14 19:34, Aaron Schrab wrote:
Instead I'll give a method to help avoid the problem. For awhile now, anytime I'm using calling a command with enough options to warrant line continuation I build them up in an array instead. This allows splitting across lines without using a backslash at the end, and *actually* allows comments.

This also allows the arguments to be build up gradually with some being added conditionally.

The below actually works as written including the comments:

    args=(
      -re 's/abc/xyx/' # End of the alphabet is better
      -re 's/123/456/' # Bigger numbers are better
      # -re 's/foo/bar/' # Disable for now
    )

    if [ -r /some/file ]
    then
      args+=(-re 's/new/old/')
    fi

    echo abc123 | sed "${args[@]}"

For zsh you can just use $args to reference it, the way I wrote it above will work in bash as well.

That is most clever!  As it is, when I have a long set of seds, I copy the whole thing, comment it, and then add comments (within the comments) so as to inform myself what the seds are doing since I tend to forget all the syntactic details. You method permits the comments where they naturally belong.  Mind, it seems to me that comments after a line wrap would be easy to implement but that's not what we have.  Thanks again.






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