0

I'm want to remove all newline characters in a file between two XML tags using sed on HP-UX in KornShell (ksh). I used sed and got what I want except that when I turn the multi-line command to a single line (one-liner) it fails.

The file looks like this.

<NAME>
  <FNAME>John</FNAME>
  <LNAME>Doe</FNAME>
</NAME>
<NAME>
  <FNAME>Jane</FNAME>
  <LNAME>Doe</FNAME>
</NAME>

The following command in a script

sed  '/<NAME/,/<\/NAME/{
{H;x;s/\n[ ^I]*//;x;};
/<NAME/{s/[ ^I]*//;h;};
/<\/NAME/{x;p;};
d;}' xmlfile

gives me exactly what I want

<NAME><FNAME>John</FNAME><LNAME>Doe</FNAME></NAME>
<NAME><FNAME>Jane</FNAME><LNAME>Doe</FNAME></NAME>

However, when I turn that into a one-liner like

sed  '/<NAME/,/<\/NAME/{{H;x;s/\n[ ^I]*//;x;};/<NAME/{s/[ ^I]*//;h;};/<\/NAME/{x;p;};d;}' xmlfile

it fails and says

sed: There are too many '{'.

I don't know why and hope someone can tell me what the error message means and how to fix it. Don't preach readability; this is more out of curiosity.

The actual file I want to use this on is rather large around 15MB and there are approximately 250 lines between every <TAG> and </TAG> which I'm converting into one line. I pick sed but am open to awk or any other best-tool-for-the-job suggestions, but still would want to know why I'm getting the above error message.

Praveen Lobo
  • 6,956
  • 2
  • 28
  • 40
  • cannot reproduce, works fine on MSYS. Have you tried inserting a semi-colon between the 2 consecutive `{` ? – Jean-François Fabre Oct 27 '16 at 22:00
  • I think I'm not getting what I want with multiline either! The last name tag is ending with ``. Will check tomorrow; HP-UX only at work. – Praveen Lobo Oct 27 '16 at 22:20
  • HP-UX was at work for me 18 years ago. So I cannot check either :) good luck. – Jean-François Fabre Oct 27 '16 at 22:25
  • Your one-line script works here with both GNU sed and BSD sed. – Thor Oct 27 '16 at 22:34
  • 1
    try `perl -ne 'if(//../<\/NAME>/){s/^ *|\n//g; $s .= $_; if(/<\/NAME>/){print "$s\n"; $s=""}}else{ print }' xmlfile` and `awk '//{f=1} f{gsub("^ *", ""); s = s $0} /<\/NAME>/{f=0; gsub("\n", "", s); print s; s = ""; next} !f' xmlfile` – Sundeep Oct 28 '16 at 01:47
  • @Jean-FrançoisFabre tried a semi-colon as you said. Didn't work. Also that multiline is working just fine. There is a typo in xmlfile; garbage in garbage out. – Praveen Lobo Oct 28 '16 at 03:53

0 Answers0