Link to home
Create AccountLog in
Avatar of hypervisor
hypervisor

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Tintin
Tintin

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

A sample (dummy) .xml file will help as well, can you post one?