[prog] replacement strings

Miriam English mim at miriam-english.org
Wed May 26 14:35:45 UTC 2010


Sam Watkins wrote:

> This perl one-liner can do it.  It chomps off all the newlines, but if it sees
> a blank line, it outputs a double newline.
> 
>   perl -pe 'chomp; print "\n\n" if $_ eq "";'
> 
> If you want it to deal with poetry, I guess you could indent your poetry and do
> like:
> 
>   perl -pe 'chomp unless /^ /; print "\n\n" if $_ eq "";'
> 
> or otherwise think of how you want that to work.

This is lovely! I'd started to cobble together a bash script using read 
and sed, but I'm amazed at how concise those two perl one-liners are. 
Thank you for that. I had intended to deal with poetry by finding the 
average line length then not wrapping lines that are significantly less 
than that length, but that has dangers and complications. Indenting 
might be a simpler way to deal with it. Thanks for the suggestion.


> I guess fmt wasn't written with that in mind, but it is consistent with its
> function.  It's a bit bogus to have a limit like that, I thought the GNU tools
> got rid of such arbitrary limits.  It should have an "unwrap" option where it
> never breaks a paragraph, then it would do what you want.  Might be a case for
> patching fmt, it is open-source after all.

Yes. I perused the source and found the spot where the line lengths are 
set. There is a note mentioning the difficulty of extending those 
lengths indefinitely due to the way the program was written, followed by 
a note "FIXME: Remove these arbitrary limits." I'll see if I can see a 
way to do that. If I succeed (I'm very rusty at C -- it has been many 
years) then I'll pass it back to the folks at Gnu.

> You could try the perl modules Text::Format or Text::Wrap too.

Thanks for that.

> In vim and other decent editors, you can apply any script to a chunk of text.
> For example, in vim, highlight some text with V (visual line mode) then type
> :!fmt -w 20
> 
> You can undo it with u.

Hmmm... tried that and I just end up getting mixed up with weird results 
and no reformatting. Seems whenever I try to use vim I get stuck in 
strange modes where I can't exit back to something normal. Its lack of 
feedback can be very frustrating.

Thanks for all the suggestions.

	- Miriam

-- 
If you don't have any failures then you're not trying hard enough.
  - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blog: http://miriam_e.livejournal.com


More information about the Programming mailing list