Replace [|] in perl

Hi
I have a file newfile.txt in perl. Its content is

22533978[|]06-Dec-11 03.04.14.000000 PM[|]99999[|]01-May-12 03.28.21.000000 PM[|]22[|]2212{|}
29055978[|]03-Jan-12 01.24.24.000000 AM[|]Bank_CreateCustomerDET[|]01-May-12 11.14.53.000000 AM[|]23[|]2382{|}

I have to replace [|] by |
and {|} by newline character
I tried with sed but it doesnt work and it replaces [|] by |||.

Can someone help with this. Thanks in advance.

Hi irudayaraj,

One way. It seems ugly because there is to escape all characters inside the substitution command:

$ perl -pe 's/\[\|\]/|/g; s/\{\|\}/\n/g' newfile.txt
22533978|06-Dec-11 03.04.14.000000 PM|99999|01-May-12 03.28.21.000000 PM|22|2212

29055978|03-Jan-12 01.24.24.000000 AM|Bank_CreateCustomerDET|01-May-12 11.14.53.000000 AM|23|2382


Thanks it works. Can i know how it works?

Try this,

nawk '{gsub(/\[../,"|");gsub(/\{../,"\n")}1' txt

Regards,
Indu

Yes. See Regexp Quote-Like Operators in perlop.

The substitution command replaces first part with second part s/first_part/second_part/. The 'g' flag is to apply the substitution many times for each line.

So, first substitution command replaces [|] with |, and second one {|} with newline ( \n). It is simple, isn't it?

1 Like

using Python32:-

import re

fin = open('newfile.txt','r')
fout = open('file_out.txt','w+')
for line in fin:
	print(line.replace(r'[|]','|').replace(r'{|}','\n'),file=fout)

	
fin.close()
fout.close()

o/p:

22533978|06-Dec-11 03.04.14.000000 PM|99999|01-May-12 03.28.21.000000 PM|22|2212


29055978|03-Jan-12 01.24.24.000000 AM|Bank_CreateCustomerDET|01-May-12 11.14.53.000000 AM|23|2382


BR
:D:D:D