Replacing Last occurance of & from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing Last occurance of & from a string
# 1  
Old 05-13-2008
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:

Code:
- <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:

Code:
- <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

Last edited by Yogesh Sawant; 05-14-2008 at 02:24 AM.. Reason: added code tags
# 2  
Old 05-13-2008
Try this:

Code:
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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

Search & Replacing text within a file

Hi all! I'm a newbie and I'm writing a script which will ask a user for data to search. I will then search for this data using grep and displaying this data back to the screen for the user to see. The user will then enter new data to replace the data searched. Now how do I replace this data... (4 Replies)
Discussion started by: macastor
4 Replies

3. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

4. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

5. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

8. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

9. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question