Replacing Last occurance of & from a string

Hi All,

Could anyone help me out in the below requirement:

I have a text(XML) file like this:

- <Dim2>
  <Properties Name="[Customers]" State="2" ShowHir="-1" ApplyFilter="-1" ExpandToLevel="1" BreakHierType="1" MaxDepth="1" SlicerSelectOptions="1" ShowLeaf="0" HasGroup="0" /> 
  <Expanded Value="Pn0102{}[Customers].[Parent Name].&[All Customers]{}0{}[Customers].[All Customers]{}0" /> 
  <Hidden Value="Pn0102{}[Customers].[Parent Name].&[C_Unmatched Outpayments]{}0{}[Customers].[Parent Name].&[ITXC Corporation]{}0{}[Customers].[Parent Name].&[Teleglobe Corporation]{}0{}[Customers].[Parent Name].&[Teleglobe Inter-Company]{}0" /> 
- <Filter>
  <Properties DimName="[Customers]" UseFilter="0" /> 
- <ByName>
  <Properties Count="1" /> 
  <Names Value="Pn0102{}Members{}0" /> 
- <Item1>
  <Properties Name="Members" FilterType="0" Members="Pn0102{}[Customers].[Parent Name].&[All Customers].&[NEOTEL / NEOTEL (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/CELL-C (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/DIGITAL INDABA]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/LEVIN GLOBAL]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/MTN (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/SYRINX COMMUNICATIONS INTERNATIONAL (PTY) LTD]{}0{}[Customers].[Parent Name].&[All Customers].&[NEOTEL/VODACOM (PRIME)]{}0" IncludeMember="-1" ExcludeFilter="0" AndFilter="0" Prefix="0" Suffix="0" Enabled="-1" /> 
  <ValueFilter Activated="0" IsGridFilter="0" AxisType="0" Members="Pn0102{}" FilterType0="8" Value0="5" Value10="0" IsAnd0="0" IsVisible0="-1" IsEnabled0="-1" MaxValue0="10" MinValue0="1" Step0="1" Enabled0="0" FilterType1="8" Value1="0" Value11="0" IsAnd1="0" IsVisible1="0" IsEnabled1="-1" MaxValue1="10" MinValue1="1" Step1="1" Enabled1="0" FilterType2="8" Value2="0" Value12="0" IsAnd2="0" IsVisible2="0" IsEnabled2="-1" MaxValue2="10" MinValue2="1" Step2="1" Enabled2="0" /> 
  <ValueFilterSort Direction="0" Members="Pn0102{}" /> 
  </Item1>
  </ByName>
  </Filter>
  </Dim2>

In this file, i need to do the following:

Find the occurance of [Customers].]{}0 and replace the last occurance of & between [Customers].]{}0

where * could be any text

and hence my output should look like:

- <Dim2>
  <Properties Name="[Customers]" State="2" ShowHir="-1" ApplyFilter="-1" ExpandToLevel="1" BreakHierType="1" MaxDepth="1" SlicerSelectOptions="1" ShowLeaf="0" HasGroup="0" /> 
  <Expanded Value="Pn0102{}[Customers].[Parent Name].[All Customers]{}0{}[Customers].[All Customers]{}0" /> 
  <Hidden Value="Pn0102{}[Customers].[Parent Name].[C_Unmatched Outpayments]{}0{}[Customers].[Parent Name].[ITXC Corporation]{}0{}[Customers].[Parent Name].[Teleglobe Corporation]{}0{}[Customers].[Parent Name].[Teleglobe Inter-Company]{}0" /> 
- <Filter>
  <Properties DimName="[Customers]" UseFilter="0" /> 
- <ByName>
  <Properties Count="1" /> 
  <Names Value="Pn0102{}Members{}0" /> 
- <Item1>
  <Properties Name="Members" FilterType="0" Members="Pn0102{}[Customers].[Parent Name].&[All Customers].[NEOTEL / NEOTEL (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/CELL-C (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/DIGITAL INDABA]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/LEVIN GLOBAL]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/MTN (PRIME)]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/SYRINX COMMUNICATIONS INTERNATIONAL (PTY) LTD]{}0{}[Customers].[Parent Name].&[All Customers].[NEOTEL/VODACOM (PRIME)]{}0" IncludeMember="-1" ExcludeFilter="0" AndFilter="0" Prefix="0" Suffix="0" Enabled="-1" /> 
  <ValueFilter Activated="0" IsGridFilter="0" AxisType="0" Members="Pn0102{}" FilterType0="8" Value0="5" Value10="0" IsAnd0="0" IsVisible0="-1" IsEnabled0="-1" MaxValue0="10" MinValue0="1" Step0="1" Enabled0="0" FilterType1="8" Value1="0" Value11="0" IsAnd1="0" IsVisible1="0" IsEnabled1="-1" MaxValue1="10" MinValue1="1" Step1="1" Enabled1="0" FilterType2="8" Value2="0" Value12="0" IsAnd2="0" IsVisible2="0" IsEnabled2="-1" MaxValue2="10" MinValue2="1" Step2="1" Enabled2="0" /> 
  <ValueFilterSort Direction="0" Members="Pn0102{}" /> 
  </Item1>
  </ByName>
  </Filter>
  </Dim2>

This is very much required to fix an issue asap. Early resolution would be very very helpful to me.

Thanks,
Vivekshady

Try this:

perl -pe 's/([[]Customers[]].*?)&([^&]+?{}0)/$1$2/g' input

This part matches the [Customers] prefix, saved in $1. The *? means not to be greedy, otherwise it would match the whole line:

([[]Customers[]].*?)

This part matches the final &:

&

This part matches the remaining non-& characters before the next {}0, saved in $1. Once again, +? means one or more occurrences, but not greedily:

([^&]+?{}0)