Solved

linux shell script that executes sed substitution based on mysql query

Posted on 2010-11-22
6
484 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Utilizing an array to gracefully append to a list of EmailAddresses
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now