Solved

Modify shell script to find and replace text strings in file

Posted on 2011-09-29
13
292 Views
Last Modified: 2012-05-12
I have the following script:

#!/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

Open in new window


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!
0
Comment
Question by:hypervisor
  • 6
  • 6
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 36817734
perl -i.bak -pe 's/text/replace/g' $i
0
 

Author Comment

by:hypervisor
ID: 36817761
How does that fit into the script I already posted?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36817806
After the "for i" and before the file is sent to the command "java"
0
 

Author Comment

by:hypervisor
ID: 36817862
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

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 36818081
Interchange lines 3 and 4.  

do should come immediately after for
0
 
LVL 84

Expert Comment

by:ozo
ID: 36818088
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 36818090
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
0
 

Author Comment

by:hypervisor
ID: 36818336
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

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 36818392
what was in the file before the s/ay/CR/ and what was in the file after the s/ay/CR/ ?
0
 

Author Comment

by:hypervisor
ID: 36818401
"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
0
 

Author Comment

by:hypervisor
ID: 36818640
Actually, the following script works:
'
#!/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

Open in new window


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?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 36818673

perl -i.bak -pe 's/ay/CR/g;s/search2/replace2/g;s/search3/replace3/g' $i
0
 

Author Comment

by:hypervisor
ID: 36818722
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remove duplicates from the csv file 13 102
Perl Scripting from a shell script with if and for 4 83
use google analytics code in perl script 2 64
Perl modules on linux ec2 3 96
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

15 Experts available now in Live!

Get 1:1 Help Now