procmail
[Top] [All Lists]

Re: Problem with lists expansion

1997-04-30 14:46:00
The problem was how to convert newlines to pipes without leaving a trailing
pipe at the end of the output (because of the closing newline in the input).

Andrew C. Feren suggested,

|       Rather than using [sed's] y operator ... why not the following:
| 
| sed '$q;s/$/|/' 
| 
|       I can't think of a sed implementation that won't handle that.

The y operator won't work because the closing newline of the pattern space
isn't an available character for y'ing in sed's viewpoint.

However, no sed implementation will get proper results from what Andrew re-
commended.  Some don't grok semicolons and will exit with an error; that's
easily rectified,

  sed '$ !s/$/|/'

but even at that it still preserves the newlines; it adds a pipe at the end
of every line except the last one, but the newlines remain after each pipe. 
You could do this,

 variable=`sed '$ !s/$/|/' $file | tr -d \\12`

or this variation on Andrew's suggestion might work:

 variable=`sed '$ !s/$/|\\/' $file`

since it would append a pipe and a backslash to every line except the last,
and then the newlines would be escaped by backslashes when $variable appears
in a procmail regexp (with "$" interpretation, of course).

One could also use the sed code I posted Tuesday:

 variable=`sed -e :a -e N -e '$ !ba' -e 's/\n\n*/|/g' $file`

though that would, I see now, barf if the input file has only one line (or
[as would all these suggestions] if there is a leading or trailing blank
line, though medial blank lines don't hurt).  Well, ok then:

 variable=`sed -e /./!d -e :a -e '$ !N' \
               -e '$ !ba' -e 's/\n\n*\(.\)/|&/g' $file`


<Prev in Thread] Current Thread [Next in Thread>