sed and $RANDOM in bash script

hello world,

This script seems to not function in several areas. The value of $myrandom does not expand where I want in the see commands near the bottom. Originally I had each line in single quotes and the variable in soignée quotes as well, but that didn't work. I've tried many combinations of quotes and escaping characters but to no avail.

The idea is if one or more than one .xml files exist in the zzzzzz directory, for the scripts to:
come up with a random number,
use the filename before the first "-" and get rid of the path (this works)
go do the sed stuff
then do the stuff under the seds.

the $myrandom is the issue, where the value is not written in the file for the SPS, PROC, and RP lines.

If I can get that working I should be a lot closer than I am now.....

Any help would be appreciated.
#!/bin/bash
for i in /zzzzzz/*.xml
do

if [ -e $i ]; then
myrandom=$RANDOM

echo $myrandom

patID1=$(echo $i | cut -f1 -d'-')
PATID=$(basename $patID1)

        sed -i "" -e "s/<attr tag='"00400006"'>wwwww<\/attr>/<attr tag='"00400006"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00400007"'>aaaaa<\/attr>/<attr tag='"00400007"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00400011"'>bbbbb<\/attr>/<attr tag='"00400011"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080102"'>ccccc<\/attr>/<attr tag='"00080102"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080104"'>ddddd<\/attr>/<attr tag='"00080104"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00400012"'>eeeee<\/attr>/<attr tag='"00400012"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00321070"'>fffff<\/attr>/<attr tag='"00321070"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00321060"'>ggggg<\/attr>/<attr tag='"00321060"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080102"'>hhhhh<\/attr>/<attr tag='"00080102"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080104"'>iiiii<\/attr>/<attr tag='"00080104"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00321032"'>unknown<\/attr>/<attr tag='"00321032"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00321033"'>jjjjj<\/attr>/<attr tag='"00321033"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080090"'>kkkkk<\/attr>/<attr tag='"00080090"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00380300"'>lllll<\/attr>/<attr tag='"00380300"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00380500"'>mmmmm<\/attr>/<attr tag='"00380500"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00102000"'>nnnnn<\/attr>/<attr tag='"00102000"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00102110"'>ooooo<\/attr>/<attr tag='"00102110"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00380050"'>ppppp<\/attr>/<attr tag='"00380050"'><\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080060"'>11<\/attr>/<attr tag='"00080060"'>22<\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00400009"'>SPS-1234<\/attr>/<attr tag='"00400009"'>SPS-$myrandom<\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00080100"'>PROC-1234<\/attr>/<attr tag='"00080100"'>PROC-$myrandom<\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00401001"'>RP-1234<\/attr>/<attr tag='"00401001"'>RP-$myrandom<\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00100020"'>Patient ID<\/attr>/<attr tag='"00100020"'>$PATID<\/attr>/" $i
        sed -i "" -e "s/<attr tag='"00400012"'>qqqqq<\/attr>/<attr tag='"00400012"'><\/attr>/" $i


java -jar /xxx/x/bin/xx $i
wait
mv $i $i.parsed
fi
done

Open in new window

hypervisorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
Hello hypervisor, I'd make a few changes to the sed lines, for example this one:

sed -i "" -e "s/<attr tag='"00400009"'>SPS-1234<\/attr>/<attr tag='"00400009"'>SPS-$myrandom<\/attr>/" $i

Open in new window


I'd change to:

sed -i -e "s/<attr tag='\"00400009\"'>SPS-1234<\/attr>/<attr tag='\"00400009\"'>SPS-$myrandom<\/attr>/" $i

Open in new window


I've been testing with these changes, $myrandom is substituted just fine and the in place editing (-i) is working as well. Please test this and let me know what else goes wrong, if any. How is your RANDOM filled btw? I don't see it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TintinCommented:
Do you attribute tag values have single or double quotes around them?

Here's a cleaned up version of your code with all the redundant stuff taken out


#!/bin/bash
cd /zzzzzz

for file in *.xml
do


  myrandom=$RANDOM
  PATID=$(echo $i | cut -f1 -d'-')


  sed -i -e "s#<attr tag='00400006'>wwwww</attr>#<attr tag='00400006'></attr>#" \
   -e "s#<attr tag='00400007'>aaaaa</attr>#<attr tag='00400007'></attr>#" \
   -e "s#<attr tag='00400011'>bbbbb</attr>#<attr tag='00400011'></attr>#" \
   -e "s#<attr tag='00080102'>ccccc</attr>#<attr tag='00080102'></attr>#" \
   -e "s#<attr tag='00080104'>ddddd</attr>#<attr tag='00080104'></attr>#" \
   -e "s#<attr tag='00400012'>eeeee</attr>#<attr tag='00400012'></attr>#" \
   -e "s#<attr tag='00321070'>fffff</attr>#<attr tag='"00321070'></attr>#" \
   -e "s#<attr tag='00321060'>ggggg</attr>#<attr tag='00321060'></attr>#" \
   -e "s#<attr tag='00080102'>hhhhh</attr>#<attr tag='00080102'></attr>#" \
   -e "s#<attr tag='00080104'>iiiii</attr>#<attr tag='00080104'></attr>#" \
   -e "s#<attr tag='00321032'>unknown</attr>#<attr tag='00321032'></attr>#" \
   -e "s#<attr tag='00321033'>jjjjj</attr>#<attr tag='00321033'></attr>#" \
   -e "s#<attr tag='00080090'>kkkkk</attr>#<attr tag='00080090'></attr>#" \
   -e "s#<attr tag='00380300'>lllll</attr>#<attr tag='00380300'></attr>#" \
   -e "s#<attr tag='00380500'>mmmmm</attr>#<attr tag='00380500'></attr>#" \
   -e "s#<attr tag='00102000'>nnnnn</attr>#<attr tag='00102000'></attr>#" \
   -e "s#<attr tag='00102110'>ooooo</attr>#<attr tag='00102110'></attr>#" \
   -e "s#<attr tag='00380050'>ppppp</attr>#<attr tag='00380050'></attr>#" \
   -e "s#<attr tag='00080060'>11</attr>#<attr tag='00080060'>22</attr>#" \
   -e "s#<attr tag='00400009'>SPS-1234</attr>#<attr tag='00400009'>SPS-$myrandom</attr>#" \
   -e "s#<attr tag='00080100'>PROC-1234</attr>#<attr tag='00080100'>PROC-$myrandom</attr>#" \
   -e "s#<attr tag='00401001'>RP-1234</attr>#<attr tag='00401001'>RP-$myrandom</attr>#" \
   -e "s#<attr tag='00100020'>Patient ID</attr>#<attr tag='00100020'>$PATID</attr>#" \
   -e "s#<attr tag='00400012'>qqqqq</attr>#<attr tag='00400012'></attr>/" $file


  java -jar /xxx/x/bin/xx $file

  mv $file $file.parsed

done

Open in new window

0
Gerwin Jansen, EE MVETopic Advisor Commented:
A sample (dummy) .xml file will help as well, can you post one?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.