Link to home
Start Free TrialLog in
Avatar of mcgilljd
mcgilljd

asked on

linux shell script that executes sed substitution based on mysql query

hard coded sed substitution
cat $OUTFILE_1 |sed -e 's/ABC1/123/g' -e 's/DEF1/456/g'

Is there a way to look up the values from a mysql table.  something like select field2 where field1 like '%1';  that could be put in a result set array to dynamically substitute all the field1 with field2?
Avatar of medvedd
medvedd

If you can get values from mysql and put them into shell variables, you can do something like this:

sed -e 's/'"$field1"'/'"$field2"'/'
ASKER CERTIFIED SOLUTION
Avatar of Bernie Bsen
Bernie Bsen
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.... sorry replace sqlstatement according your needs:

-e"select field2  from your_table where field1  like '%$1'"

tried it in a test database on my system and forgot to replace names.....
You might be able to do something like this:

% mysql.exe -uxxxx -pYYYYYY -e "SELECT concat('sed s/', field1, '/', field2, '/;') FROM schema.table" | sh
Avatar of mcgilljd

ASKER

That works, but I was wondering if it could be done without writing to a tempfile.
Maybe
echo "cat $OUTFILE_1 |sed";for i in `mysql -BrN -ubrb  -p test -e"select object_id  from testad where object_id  like '%$1'"`; do
    echo " -e s/$1/$i/g " > $outfile
forgot the done.

echo "cat $OUTFILE_1 |sed";for i in `mysql -BrN -ubrb  -p test -e"select object_id  from testad where object_id  like '%$1'"`; do
    echo " -e s/$1/$i/g ";done > $outfile