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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.... 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.....
-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
% mysql.exe -uxxxx -pYYYYYY -e "SELECT concat('sed s/', field1, '/', field2, '/;') FROM schema.table" | sh
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
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
ASKER
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
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
sed -e 's/'"$field1"'/'"$field2"'