hypervisor
asked on
Modify shell script to find and replace text strings in file
I have the following script:
After the "for i" and before the file is sent to the command "java", I would like to search the file for a string of text and replace the string of text. I may need to do this multiple times. Can someone provide an example of how this could be done?
Thanks!
#!/bin/bash
for i in /DVMAXMWL/*.xml
do
java -jar /securePACS/dcm4chee-2.15.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done
After the "for i" and before the file is sent to the command "java", I would like to search the file for a string of text and replace the string of text. I may need to do this multiple times. Can someone provide an example of how this could be done?
Thanks!
perl -i.bak -pe 's/text/replace/g' $i
ASKER
How does that fit into the script I already posted?
After the "for i" and before the file is sent to the command "java"
ASKER
I'm trying this but it doesn't work - replaced "text" with text to find and "replace" with text we want to replace.
#!/bin/bash
for i in /DVMAXMWL/*.xml
perl -i.bak -pe 's/Performing^Physician/null/g' $i
do
java -jar /securePACS/dcm4chee-2.15.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done
Interchange lines 3 and 4.
do should come immediately after for
do should come immediately after for
In a regular expression, ^ matches the start of the line, but -p reads a line at a time, so there will never be a start of a line there.
If you want to match a literal ^ you can quote it with \^
also, the do is part of the for i, so the command should be inside the do
If you want to match a literal ^ you can quote it with \^
also, the do is part of the for i, so the command should be inside the do
In a regular expression, ^ matches the start of the line, but -p reads a line at a time, so there will never be a start of a line there.
If you want to match a literal ^ you can quote it with \^
also, the do is part of the for i, so the command should be inside the do
If you want to match a literal ^ you can quote it with \^
also, the do is part of the for i, so the command should be inside the do
ASKER
In this instance, I am trying to replace "ay" with "CR" - it didn't work. Thoughts?
#!/bin/bash
for i in /DVMAXMWL/*.xml
do
perl -i.bak -pe 's/ay/CR/g' $i
java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done
what was in the file before the s/ay/CR/ and what was in the file after the s/ay/CR/ ?
ASKER
"ay" was in the file before and "ay" was in the file after.
The actual line of text which includes "ay" reads -
Modality=ay
We wan't to replace with CR
The actual line of text which includes "ay" reads -
Modality=ay
We wan't to replace with CR
ASKER
Actually, the following script works:
'
What if I want to run the following line many times do search and replace multiple strings?
perl -i.bak -pe 's/ay/CR/g' $i
Can I just keep repeating it?
'
#!/bin/bash
for i in /DVMAXMWL/*.xml
do
perl -i.bak -pe 's/ay/CR/g' $i
java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done
What if I want to run the following line many times do search and replace multiple strings?
perl -i.bak -pe 's/ay/CR/g' $i
Can I just keep repeating it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What if it's not just a word, but a phrase:
i.e. "Schedule Procedure Step" - if I do single words it works, if I do phrases it doesn't.
Thanks! Giving you points for this now.
i.e. "Schedule Procedure Step" - if I do single words it works, if I do phrases it doesn't.
Thanks! Giving you points for this now.