linux shell script that executes sed substitution based on mysql query

Posted on 2010-11-22
Medium Priority
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?
Question by:mcgilljd
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
LVL 16

Expert Comment

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"'/'

Accepted Solution

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

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

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

should do the job

Expert Comment

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.....
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

LVL 24

Expert Comment

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

Author Closing Comment

ID: 34190385
That works, but I was wondering if it could be done without writing to a tempfile.
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

Author Comment

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

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 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