linux script help with date calculations

Hey all -

  I am trying to put together a scrip to combine a couple of jobs.

first step is to generate a text file so that the 2nd job can run through the text file line by line to feed each row to a twiddle command.

Here is what I have so far as it is working as expected.

p.s. I couldn't call the command 'dcmqr' via the ${basedir} because it said it couldn't find it, so I gave it the whole path.

#!/bin/bash
BASEDIR=/cygdrive/c/apps/dcm4che-2.0.25-bin/bin
TWIDDLE=/cygdrive/c/apps/dcm4chee-2.17.2-mssql/bin/twiddle.sh
#Administration login and password
LOGIN=admin
PASSWORD=password
/cygdrive/c/apps/dcm4che-2.0.25-bin/bin/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=18750101-20060919 -qPatientBirthDate=18750101-19920919 -q00080061=CR | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID
INPUT=SUID
count=0
exec 3<&0 # Save stdin to file descriptor 3.
exec 0<$INPUT # Redirect standard input.
while read line
do
input1=$(echo $line | awk '{ print $1}')
echo "Deleting :" ${input1}
${TWIDDLE} -u ${LOGIN} -p ${PASSWORD} -s jnp://10.10.50.51:1099 invoke "dcm4chee.archive:service=ContentEditService" purgeStudy ${input1}
count=$( expr $count + 1 )
done
exec 0<&3 # Restore old stdin.
echo "Counter:" $count # Show deleted items

Open in new window


I am needing some help with the dates.  I don't care to edit this file every time I want to change the query.  Namely these two parameters:

-qStudyDate=18750101-20060919 -qPatientBirthDate=18750101-19920919

Is there a way to write a variable for $Studydate & $Birthdate that I can call in the above line?

$StudyDate will always need to be six years old from today
$BirthDate will always need to be 21 years old from today.

thanks
doc_jayAsked:
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.

slubekCommented:
Define shell variables as
StudyDate=`awk 'BEGIN {print "18750101-" strftime("%Y")-6 strftime("%m%d")}'`
BirthDate=`awk 'BEGIN {print "18750101-" strftime("%Y")-21 strftime("%m%d")}'`

Open in new window

0
doc_jayAuthor Commented:
thanks.  what would be an easy way to print this to a file to test its output?
0
TintinCommented:
StudyDate=$(date -d '6 years ago' +"18750101-%Y%m%d")
BirthDate=$(date -d '21 years ago' +"18750101-%Y%m%d")

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

slubekCommented:
@doc_jay:
echo $StudyDate > file

Open in new window

0
doc_jayAuthor Commented:
@slubek
When I run this code, it will only do ONE date function in the output file.  How can it be made to output both dates?

#!/bin/bash
StudyDate=$(date -d '6 years ago' +"18750101-%Y%m%d")
BirthDate=$(date -d '21 years ago' +"18750101-%Y%m%d")
echo $StudyDate > datetest
echo $BirthDate > datetest

Open in new window


I guess I just expected it to write both date ranges in the output file.  It only does $BirthDate
0
slubekCommented:
You have to use '>>' to append text to file. '>' overwrites/creates it:
#!/bin/bash
StudyDate=$(date -d '6 years ago' +"18750101-%Y%m%d")
BirthDate=$(date -d '21 years ago' +"18750101-%Y%m%d")
echo $StudyDate > datetest
echo $BirthDate >> datetest

Open in new window

0
doc_jayAuthor Commented:
@slubek
thanks - that's what I was looking for.  That works as expected.  Now you guys have the date calculation working for me, I'm running into one more problem.  I need to make more than one 'output' file and then merge them all into 'one' file.  Here is what I will have:

SUID_CR
SUID_CT
SUID_DX
SUID_MR

I'll have to merge all of the above files into one file.  What would be the easiest way to do this?

thanks & if I need to reward points at this point and start a new question I will.
0
slubekCommented:
Do as you wish. But don't forget about Tintin - his method was shorter and more elegant than mine.
How do you want the output file look like? Something like that?
$ cat SUID_CR SUID_CT SUID_DX SUID_MR > output

Open in new window

0
doc_jayAuthor Commented:
I'll make sure I give Tintin his points, as I'm using his method to calculate the date.  I am currently doing this in my script:

$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=CR| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_CR
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=CT| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_CT
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=DX| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_DX
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=MR| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_MR
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=NM| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_NM
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=PT| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_PT
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=RF| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_RF
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=SR| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_SR
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=US| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_US
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=XA| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_XA
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=OT| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_OT
$BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=RTIMAGE| grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'//> SUID_RT

Open in new window


that part will generate all of the 'SUID_xx' files.  After that I need to cat them all together into one file so that I can feed that to a different script.  So, the way to do this is the way you stated above, like in my example below?

cat SUID_CR SUID_CT SUID_DX SUID_MR SUID_NM SUID_OT SUID_PT SUID_RF SUID_RT SUID_SR SUID_US SUID_XA > SUID_ALL

Open in new window

0
doc_jayAuthor Commented:
This is working, except it is inserting 'blank lines' into the output file.  Is there a way to prevent these from being inserted or a way to take them away with cat?
0
slubekCommented:
If all filenames begin with "SUID" you can use:
$ cat SUID_* > outfile

Open in new window

But if you have too many files, bash can fail in '*' substitution. In that case you'll have to use 'for' or 'find'.
0
slubekCommented:
Do you need all these files? Maybe you can append output of all these 'dcmqr' commands directly to SUID_ALL:
$BASEDIR\dcmqr ... >> SUID_ALL
$BASEDIR\dcmqr ... >> SUID_ALL
$BASEDIR\dcmqr ... >> SUID_ALL
etc.

Open in new window

0
TintinCommented:
You can shorten your code using a for loop


for code in CR CT DX MR NM PT RF SR US XA OT RTIMAGE
do
   $BASEDIR/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$StudyDate -qPatientBirthDate=$BirthDate -q00080061=$code| grep "(0020,000D) UI #[0-9]*" | sed -e s/'(0020,000D) UI #[0-9]* \['// -e s/'\] Study Instance UID'// -e s/'] Study Instance'// -e s/'UI'// -e sed s/'U'// >SUID_$code
done

cat SUID_* >combined_file

Open in new window

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
doc_jayAuthor Commented:
@slubek & @Tintin - both of your ideas have been great and work the way I need them to.  I just need to get rid of the 'blank lines' that are being inserted into the output file from the 'cat' command.  Going to look that up now.
0
slubekCommented:
Are these blank lines after each line, or after each concatenated file?
0
doc_jayAuthor Commented:
they are after each concatenated file, I've found that

sed '/^$/d' SUID_ALL > SUID_ALL_TEMP
mv SUID_ALL_TEMP SUID_ALL

Open in new window


does the trick!

Its all working great thanks to the both of you.  Points on the way.
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
Linux

From novice to tech pro — start learning today.