[ale] bash, sed substitution brain baker

Jim Kinney jim.kinney at gmail.com
Tue Oct 27 11:20:04 EDT 2009


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}"

Now I just need to get the ${blue} eval'ed by the shell to become 2

ideas, sanity pills, public ridicule....

-- 
-- 
James P. Kinney III
Actively in pursuit of Life, Liberty and Happiness


More information about the Ale mailing list