Search and Replace

Hi!!!
I have following xml file with 3 sections.

aaa1bbb-ccc
default
aaa1bbbdd
default
0 11 23 * *

aaa2bbb-ccc
default
aaa2bbbdd
default
0 11 23 * *

aaa3bbb-ccc
default
aaa3bbbdd
default
0 15 23 * *

I want to search and replace 23 with 24 only in second section!!
How to do this???
Thanks!!!

$ ruby -00 -pne '$_.gsub!("23","24") if $. == 2' file
aaa1bbb-ccc
default
aaa1bbbdd
default
0 11 23 * *

aaa2bbb-ccc
default
aaa2bbbdd
default
0 11 24 * *

aaa3bbb-ccc
default
aaa3bbbdd
default
0 15 23 * *

Try:

awk -vRS="" 'NR==2{gsub("23","24")}1' file

I am using bash!!!
I want to simplify but i made mistake.I have xml file with 400 lines.This is quartz jobs!!!
Based on criteria I want to replace particular line with something else.
After some filtering i have this:

aaa2bbb-ccc
default
aaa2bbbdd
default
0 11 24 * *

How to replace 24 with 23 in this section and put back to original XML file.
Line 0 11 24 * * exist multiple times in other sections of xml file

Post sample of your real input data... And if you are using bash then you can use AWK too...

<name>TransferSifCen2-trigger</name> 
  <group>DEFAULT</group> 
  <job-name>TransferSifCen2Job</job-name> 
  <job-group>DEFAULT</job-group> 
  <cron-expression>0 15 22 * * ?</cron-expression>
.
.
.

<name>TransferSifCen5-trigger</name> 
  <group>DEFAULT</group> 
  <job-name>TransferSifCen5Job</job-name> 
  <job-group>DEFAULT</job-group> 
  <cron-expression>0 15 22 * * ?</cron-expression>
.
.
.

I want to replace 15 22 with 10 25 only in section with SifCen5!!!

perl -i -0pe 's/(<name>TransferSifCen5.*?<cron-expression>0 )15 22(.*?<\/cron-expression>)/${1}10 25\2/gs' file.xml

Thanks!!! It works...

---------- Post updated at 06:45 AM ---------- Previous update was at 04:55 AM ----------

what if 15 22 and 10 25 are stored in variables???Can't use $old and $new.
If I put " instead ' then replacement is done together with previous 4 lines!!!

Post the code you are using with variables.

perl -i -0pe "s/(<name>TransferSifCen5.?<cron-expression>0 ) $oldtime (.?<\/cron-expression>)/${1} $newtime \2/gs" file.xml

What does those commands return:

perl -e "print \"#$oldtime#\""
perl -e "print \"#$newtime#\""
#15 22#
#10 25#

Try:

 perl -i -0pe "s/(<name>TransferSifCen5.*?<cron-expression>0 )$oldtime(.*?<\/cron-expression>)/\${1}$newtime\2/gs" file.xml
1 Like
$ ruby -pne 'BEGIN{$/="</cron-expression>"}; $_.gsub!(/15 22/,"10 25") if $_ =~ /SifCen5/ ' file
<name>TransferSifCen2-trigger</name>
  <group>DEFAULT</group>
  <job-name>TransferSifCen2Job</job-name>
  <job-group>DEFAULT</job-group>
  <cron-expression>0 15 22 * * ?</cron-expression>
.
.
.

<name>TransferSifCen5-trigger</name>
  <group>DEFAULT</group>
  <job-name>TransferSifCen5Job</job-name>
  <job-group>DEFAULT</job-group>
  <cron-expression>0 10 25 * * ?</cron-expression>
.
.


 awk 'BEGIN{RS="";FS="\n"} /SifCen5/ {gsub(/15 22/,"10 25")}1' file.xml

if using var:

awk -v old="15 22" -v new="10 25" 'BEGIN{RS="";FS="\n"} /SifCen5/ {gsub(old,new)}1' file.xml

Thanks bartus11!!!!
This solved my problem

perl -i -0pe "s/(<name>TransferSifCen5.?<cron-expression>0 )$oldtime(.?<\/cron-expression>)/\${1}$newtime\2/gs" file.xml