Solved

linux shell script that executes sed substitution based on mysql query

Posted on 2010-11-22
6
489 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

839 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