sed simplification...

All,

I have an file in the xml format which looks like this

<source>Please wait. This may take several minutes...</source>
<translation>Please wait. This may take several minutes...</translation>

<source>Your computer must be restarted to complete setup.\nClick on 'OK' button to restart.</source>
<translation>Your computer must be restarted to complete setup.\nClick on 'OK' button to restart.</translation>

<source>'%1' can not be deleted. Close all programs\nand try again</source>
<translation>'%1' can not be deleted. Close all programs\nand try again</translation>

<source>XYZ\nCopyright 2005, ABC\nAll rights reserved</source>
<translation>XYZ\nCopyright 2005, ABC.\nAll rights reserved.</translation>

\n are the new line characters. My intention is to replace all \n with an actual new line for the translation tag alone.

I have a sed command for that

sed -e '/<translation>/{
s/\(.*\)\\n\(.*\)\\n\(.\)/\1\n\2\n\3/g
s/\(.*\)\\n\\n\(.
\)/\1\n\n\2/g
s/\(.*\)\\n\(.*\)/\1\n\2/g
}' sample.ts

The output is

[/tmp]$ sed -e '/<translation>/{  
> s/\(.*\)\\n\(.*\)\\n\(.*\)/\1\n\2\n\3/g
> s/\(.*\)\\n\\n\(.*\)/\1\n\n\2/g
> s/\(.*\)\\n\(.*\)/\1\n\2/g
> }' sample.ts
<source>Please wait. This may take several minutes...</source>
<translation>Please wait. This may take several minutes...</translation>

<source>Your computer must be restarted to complete setup.\nClick on 'OK' button to restart.</source>
<translation>Your computer must be restarted to complete setup.
Click on 'OK' button to restart.</translation>

<source>'%1' can not be deleted. Close all programs\nand try again</source>
<translation>'%1' can not be deleted. Close all programs
and try again</translation>

<source>XYZ\nCopyright 2005, ABC\nAll rights reserved</source>
<translation>XYZ
Copyright 2005, ABC.
All rights reserved.</translation>

It works fine.

Now, the sed command looks pretty complex even though it is simple. Can that sed be simplified ? I am biased towards sed because, I came up with that.

Is there a non-sed solution as well ?

Thanks,
vino

Okie. I got a simpler sed solution.

sed -e '/<translation>/{s#\\n#\n#g}' sample.ts

I guess the long hours had its toll on me..

Okie.. so now the posting goes as.. is there is a non-sed solution.

nawk '/<translation>/{ gsub("\\\\n", sprintf("\n")) }1' sample.ts

Works very well. Thanks vgersh99.

Vino

gnu sed:

$ sed -e '/<translation>/{s/\\n/\n/g}' sample.txt
<source>Please wait. This may take several minutes...</source>
<translation>Please wait. This may take several minutes...</translation>
 
<source>Your computer must be restarted to complete setup.\nClick on 'OK' button to restart.</source>
<translation>Your computer must be restarted to complete setup.
Click on 'OK' button to restart.</translation>
 
<source>'%1' can not be deleted. Close all programs\nand try again</source>
<translation>'%1' can not be deleted. Close all programs
and try again</translation>
 
<source>XYZ\nCopyright 2005, ABC\nAll rights reserved</source>
<translation>XYZ
Copyright 2005, ABC.
All rights reserved.</translation>