sed and $RANDOM in bash script

Posted on 2011-10-04
Last Modified: 2012-05-12
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.
for i in /zzzzzz/*.xml

if [ -e $i ]; then

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
mv $i $i.parsed

Open in new window

Question by:hypervisor
    LVL 37

    Accepted Solution

    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.
    LVL 48

    Expert Comment

    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

    cd /zzzzzz
    for file in *.xml
      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

    Open in new window

    LVL 37

    Expert Comment

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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now