Avatar of 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.
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

Shell Scripting

Avatar of undefined
Last Comment
Gerwin Jansen
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of 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

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

Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

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

The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo