sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

Tim Chase python.list at tim.thechases.com
Sat Aug 22 21:49:55 CEST 2009


bolega wrote:
> sed/awk/perl:

Better to post in the "sed" or "perl" mailing lists rather than a
Python list.  I saw an awk solution flew by.

> How to replace all spaces each with an underscore that occur
> before a specific string ?
> 
> I really prefer a sed one liner.

Here's a one-liner sed solution:

  sed '/to /{s//\n&/;h;s/.*\n//;x;s/\n.*//;s/ /_/g;G;s/\n//}'

There's a reason I prefer Python for these sorts of things: 
readability!  You win 5 free internets (as a stand-in for the 
"real life" you clearly don't have) if you can decipher that in 
under 20 seconds ;-)

expecting-to-see-NO CARRIER-after-typing-that'ly yers,

-tkc
+++ATH0














More information about the Python-list mailing list