[ale] bash, sed substitution brain baker

JK jknapka at kneuro.net
Tue Oct 27 14:18:27 EDT 2009


Jim Kinney wrote:
> I have a series of files that are comma delimited
> 
> abc.com,100,30,70,blue,ipv4,pizza
> 
> and there are about 4 million entries with various values in the
> fields. I need to slurp this into a db but I need to substitute fields
> 5 and 7 with their field ID instead of their value. So I some other
> shell variables in use:
> export blue='2'
> export pizza='7'
> export red='1'
> export soup='3'
> -etc.
> 
> so as an exercise in sed foo I'm trying to extract the first 4 million
> line pull, pipe it into a sed call and spit that back into a different
> db on a different machine doing a substitution on the fly as follows:
> 
> sed "s/\([a-zA-Z0-9\.]\+\),\([0-9]\+\),\([0-9]\+\),\([0-9]\+\),\([a-zA-Z0-9\.]\+\),\([a-zA-Z0-9\.]\+\),\([a-zA-Z0-9\.]\+\)/\1,\2,\3,\4,"'"${\5}"'",\6,"'"${\7}"'"/"
> 
> That pile of quotes near the end is double-single-double wrapping each
> sed variable in braces.
> 
> It _ALMOST_ works! I get back out:
> 
> abc.com,100,30,70,"${blue}",10.0.1.2,"${pizza}"


So if you take that result string (call it $STR) and do:

eval echo $STR

you ought to get the string you want.  It's not exactly pretty,
and no substitute for actually knowing WTF is up with that (I admit
I don't!), but it should work.  (I'm just trying to hill-climb from
where you are to where you wanna be, without actually parsing that
psychotic sed command.)

-- JK



More information about the Ale mailing list