Solved

linux shell script that executes sed substitution based on mysql query

Posted on 2010-11-22
6
491 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:mcgilljd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

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

sed -e 's/'"$field1"'/'"$field2"'/'
0
 
LVL 6

Accepted Solution

by:
brb6708 earned 500 total points
ID: 34190197
#! /bin/bash

tmpfile=/tmp/some_unlikely_double_filename.7455
rm -f $tmpfile
touch $tmpfile
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" >> $tmpfile
done

cat $OUTFILE_1 |sed `tr '\n' ' ' < $tmpfile`

should do the job
0
 
LVL 6

Expert Comment

by:brb6708
ID: 34190288
.... 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.....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:mankowitz
ID: 34190376
You might be able to do something like this:

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

Author Closing Comment

by:mcgilljd
ID: 34190385
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
0
 

Author Comment

by:mcgilljd
ID: 34190403
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
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question