Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

0
hypervisor
Asked:
hypervisor
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now