Yet another "sed" question

Yet another "sed" question

am 16.01.2008 15:59:22 von Robert Latest

Hello,

I've got to remove all blank lines from a file. That part is easy. But in
addition, I need to replace one or more consecutive blank lines with a
single line containing the string '.pp' UNLESS the line following the
blank line(s) starts with a period.

Yes, this is a groff preprocessor. I don't think groff can be made to treat
blank lines as paragraph separators.

robert

Re: Yet another "sed" question

am 16.01.2008 16:17:24 von Ed Morton

On 1/16/2008 8:59 AM, Robert Latest wrote:
> Hello,
>
> I've got to remove all blank lines from a file. That part is easy. But in
> addition, I need to replace one or more consecutive blank lines with a
> single line containing the string '.pp' UNLESS the line following the
> blank line(s) starts with a period.

In which case, what? Leave the consecutive blank lines? Replace them by a single
blank line? Delete all of them?

> Yes, this is a groff preprocessor. I don't think groff can be made to treat
> blank lines as paragraph separators.
>
> robert

Posting some sample input and expected output would've helped, but this is
probably what you need:

awk -v RS= '{print ( (NR==1 || /^\./) ? "" : ".pp\n") $0}' file

Regards,

Ed.

Re: Yet another "sed" question

am 16.01.2008 16:18:10 von Glenn Jackman

At 2008-01-16 09:59AM, "Robert Latest" wrote:
> Hello,
>
> I've got to remove all blank lines from a file. That part is easy. But in
> addition, I need to replace one or more consecutive blank lines with a
> single line containing the string '.pp' UNLESS the line following the
> blank line(s) starts with a period.
>
> Yes, this is a groff preprocessor. I don't think groff can be made to treat
> blank lines as paragraph separators.

I suspect there's a more elegant solution out there, but:

perl -000 -ne '
chomp;
$sep = ($. == 1 or substr($_,0,1) eq ".") ? "" : ".pp\n";
print "$sep$_\n";
' filename

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: Yet another "sed" question

am 16.01.2008 16:58:16 von Robert Latest

Ed Morton wrote:

> Posting some sample input and expected output would've helped, but this is
> probably what you need:
>
> awk -v RS= '{print ( (NR==1 || /^\./) ? "" : ".pp\n") $0}' file

Thanks. I think I need to learn awk. sed doesn't seem to do that good,
except for the 's' thingy.

robert

Re: Yet another "sed" question

am 16.01.2008 17:16:48 von Ed Morton

On 1/16/2008 9:58 AM, Robert Latest wrote:
> Ed Morton wrote:
>
>
>>Posting some sample input and expected output would've helped, but this is
>>probably what you need:
>>
>> awk -v RS= '{print ( (NR==1 || /^\./) ? "" : ".pp\n") $0}' file
>
>
> Thanks. I think I need to learn awk. sed doesn't seem to do that good,
> except for the 's' thingy.

Precisely. As someone who spent about 15 years using sed before learning awk, I
believe you should never use sed for anything other than simple substitutions.
Even though you CAN do a lot with sed, the resulting scripts are nightmarish in
their obscurity and very difficult to enhance if your requirements change since
a small change in requirements often requires a completely different approach
with the associated slew of single-character commands and separators.

If you'd like to learn awk, lurk around comp.lang.awk for a while, get the book
Effective Awk Programming, Third Edition By Arnold Robbins
(http://www.oreilly.com/catalog/awkprog3/) and force yourself to use awk for all
text manipulation problems, even if you already know how to solve them with a
mixture of sed, shell and other tools.

Ed.

Re: Yet another "sed" question

am 16.01.2008 17:18:43 von Stephane CHAZELAS

On 16 Jan 2008 14:59:22 GMT, Robert Latest wrote:
[...]
> I've got to remove all blank lines from a file. That part is easy. But in
> addition, I need to replace one or more consecutive blank lines with a
> single line containing the string '.pp' UNLESS the line following the
> blank line(s) starts with a period.
[...]

sed '
/^$/,/./{
/^[^.]/i\
..pp
}
/./!d'

--
Stephane

Re: Yet another "sed" question

am 16.01.2008 17:22:30 von Stephane CHAZELAS

On Wed, 16 Jan 2008 10:16:48 -0600, Ed Morton wrote:
[...]
> Precisely. As someone who spent about 15 years using sed before learning awk, I
> believe you should never use sed for anything other than simple substitutions.
> Even though you CAN do a lot with sed, the resulting scripts are nightmarish in
> their obscurity and very difficult to enhance if your requirements change since
> a small change in requirements often requires a completely different approach
> with the associated slew of single-character commands and separators.
>
> If you'd like to learn awk, lurk around comp.lang.awk for a while, get the book
> Effective Awk Programming, Third Edition By Arnold Robbins
> (http://www.oreilly.com/catalog/awkprog3/) and force yourself to use awk for all
> text manipulation problems, even if you already know how to solve them with a
> mixture of sed, shell and other tools.
[...]

Or make a bigger step and move to using perl (or an even bigger
to ruby?).

--
Stephane

Re: Yet another "sed" question

am 16.01.2008 17:28:18 von Ed Morton

On 1/16/2008 10:22 AM, Stephane Chazelas wrote:
> On Wed, 16 Jan 2008 10:16:48 -0600, Ed Morton wrote:
> [...]
>
>>Precisely. As someone who spent about 15 years using sed before learning awk, I
>>believe you should never use sed for anything other than simple substitutions.
>>Even though you CAN do a lot with sed, the resulting scripts are nightmarish in
>>their obscurity and very difficult to enhance if your requirements change since
>>a small change in requirements often requires a completely different approach
>>with the associated slew of single-character commands and separators.
>>
>>If you'd like to learn awk, lurk around comp.lang.awk for a while, get the book
>>Effective Awk Programming, Third Edition By Arnold Robbins
>>(http://www.oreilly.com/catalog/awkprog3/) and force yourself to use awk for all
>>text manipulation problems, even if you already know how to solve them with a
>>mixture of sed, shell and other tools.
>
> [...]
>
> Or make a bigger step and move to using perl (or an even bigger
> to ruby?).
>

Yes. I've never personally found a need to do that and find their syntaxes (esp.
perl) obscure, and unlike awk they're not generally available on all the
machines I use but I do believe they have some capabilities unrelated to text
manipulation that awk doesn't since awk's a tool/language designed specifically
for text manipulation.

Ed.

Re: Yet another "sed" question

am 17.01.2008 04:07:57 von Maxwell Lol

Robert Latest writes:

> Hello,
>
> I've got to remove all blank lines from a file. That part is easy. But in
> addition, I need to replace one or more consecutive blank lines with a
> single line containing the string '.pp' UNLESS the line following the
> blank line(s) starts with a period.

Well, one way is
uniq | sed 's/^ *$/.pp/'