I’m writing a script to convert some code. I have a sed command that performs the proper conversion however I need to use the -n command to prevent automatic printing of pattern space to suppress double printing. Yet this then prevents printing any of the other lines that I want to be output. This is the script I’m using.
sed -i -r 's/^[ ]*Class[(]"(I((.([A-Za-z]+)))+)"[ ]*,[ ]*(I((.[A-z]+)+)),[ ]*\{/ class \4 extends \5 { /p
/^[ ]*Class[(]"(I((.([A-Za-z]+)))+)"[ ]*,/ {
N
s/ *\n/ /
s/^[ ]*Class[(]"(I((.([A-Za-z]+)))+)"[ ]*,[ ]*(I((.[A-z]+)+)),[ ]*\{*/ class \4 extends \5 { \
/p
}
'
A test input file can be found here: https://www.dropbox.com/s/h9vly1hwtqtl8te/TestText.txt
the two different results from having/not having the -n switch in the script can be found here:https://www.dropbox.com/s/f0g8horafwial1x/TestText.txt.ts_result1.txt
and here: https://www.dropbox.com/s/ekualxj7foapgml/TestText.txt.ts_result2.txt
with the desired results being found here: https://www.dropbox.com/s/3510oh899fh0ji2/TestText.txt.ts_resultdesired.txt
Is it possible to obtain the desired results?
I’m running
$ sed –version
GNU sed version 4.2.1
Another way to prevent double-printing is to place a
dafter the last print command (allowing you to remove the-n). Also, because you have a conditional after the print, you can use instead thetcommand, that will branch to a label (in this case thedonelabel before thedcommand) when the previous substitute succeeds (ie. it printed something already).(Note: Not tested this though…)