[ale] sed head scratcher

JK jknapka at kneuro.net
Tue Aug 4 14:33:52 EDT 2009


Jim Kinney wrote:
> breakout the greybeards....
> 
> 
> I have a pile of text lines that need to have a certain portion
> transposed from upper case (windows dweebs did the hostnames) to lower
> case.
> 
> So many lines like:
> 
> 192.168.0.2 MACHINE102
> 192.168.3.4  MACHINE304 FredsBox
> 10.0.2.3   MACHINE599  TestingSystemB
> etc
> 
> So varying IP address then varying spaces then upper case name with
> digits then varying spaces and sometimes followed by other name with
> mixed case.
> 
> I want to ONLY lower case the names after the IP address, not anything
> else in the line.
> 
> Here's what I have so far:
> 
> cat /etc/hosts | sed '/ [A-Z]{7}[0-9]{1,3}/ y/[A-Z]/[a-z]/'
> 
> seems like it should work but it only replaces the 'A' with 'a' and it
> does it anywhere in the line.


Disclaimer 1: I just scrambled the keycaps on my laptop, so this message
may make absolutely no sense whatsoever.

Disclaimer 2: I Am Not A sed Expert, though I've used it on occasion
(and have to re-read the manpage each time).

That said:

(1) First regexp selects lines to change, but does not restrict the
scope of the following command within the selected lines.  The
command itself, IIRC, must do the work to restrict itself to a particular
region of interest within the line it's operating on. (Accomplishing
this is left as an exercise for the reader, but I'm pretty sure that
in this case it would involve capture groups in the "to change"
regexp and capture-group backrefs in the replacement :-)

(2) Assuming sed substitutions are like vi ones, wouldn't you need
a "g" (global) qualifier to make the subst cmd do more than a single
substitution?

-- JK




More information about the Ale mailing list