0

I have a script that curl a command to sms gateway to send sms and get xml as response. Each response are appended to a single file for future xsltproc process for creating csv.

A single response is like:

<?xml version='1.0' encoding='ISO-8859-1' ?><REPLY><PARAMETER>OK</PARAMETER><LOGIN>SUCCESSFULL</LOGIN><PUSHAPI>ACTIVE</PUSHAPI><STAKEHOLDERID>OK</STAKEHOLDERID><PERMITTED>OK</PERMITTED><SMSINFO><MSISDN>xxxxxxxxxxxxx</MSISDN><SMSTEXT>Test+SMS+Check</SMSTEXT><CSMSID>113426778</CSMSID><REFERENCEID>2020050321383271896124009</REFERENCEID></SMSINFO></REPLY> 

Now each time the script runs it appends the same response with separate values with duplicate <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> and </REPLY>

so after 2nd time execution the file will be:

<?xml version='1.0' encoding='ISO-8859-1' ?><REPLY><PARAMETER>OK</PARAMETER><LOGIN>SUCCESSFULL</LOGIN><PUSHAPI>ACTIVE</PUSHAPI><STAKEHOLDERID>OK</STAKEHOLDERID><PERMITTED>OK</PERMITTED><SMSINFO><MSISDN>xxxxxxxxxxxxx</MSISDN><SMSTEXT>Test+SMS+Check</SMSTEXT><CSMSID>113426778</CSMSID><REFERENCEID>2020050321383271896124009</REFERENCEID></SMSINFO></REPLY><?xml version='1.0' encoding='ISO-8859-1' ?><REPLY><PARAMETER>OK</PARAMETER><LOGIN>SUCCESSFULL</LOGIN><PUSHAPI>ACTIVE</PUSHAPI><STAKEHOLDERID>OK</STAKEHOLDERID><PERMITTED>OK</PERMITTED><SMSINFO><MSISDN>xxxxxxxxxxxxx</MSISDN><SMSTEXT>Test+SMS+Check</SMSTEXT><CSMSID>113426778</CSMSID><REFERENCEID>2020050321383271896124009</REFERENCEID></SMSINFO></REPLY> 

which have 2 <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> and </REPLY>.

But xsltproc can not process multiple <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> and </REPLY>

So I need to remove all <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> and </REPLY> and just keep or add a <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> at beginning of file and </REPLY> end of file while final processing.

So my file while processing for xsltproc after 2nd run should be like:

<?xml version='1.0' encoding='ISO-8859-1' ?><REPLY><PARAMETER>OK</PARAMETER><LOGIN>SUCCESSFULL</LOGIN><PUSHAPI>ACTIVE</PUSHAPI><STAKEHOLDERID>OK</STAKEHOLDERID><PERMITTED>OK</PERMITTED><SMSINFO><MSISDN>xxxxxxxxxxxxx</MSISDN><SMSTEXT>Test+SMS+Check</SMSTEXT><CSMSID>113426778</CSMSID><REFERENCEID>2020050321383271896124009</REFERENCEID></SMSINFO><PARAMETER>OK</PARAMETER><LOGIN>SUCCESSFULL</LOGIN><PUSHAPI>ACTIVE</PUSHAPI><STAKEHOLDERID>OK</STAKEHOLDERID><PERMITTED>OK</PERMITTED><SMSINFO><MSISDN>xxxxxxxxxxxxx</MSISDN><SMSTEXT>Test+SMS+Check</SMSTEXT><CSMSID>113426778</CSMSID><REFERENCEID>2020050321383271896124009</REFERENCEID></SMSINFO></REPLY> 

So how to remove all <?xml version='1.0' encoding='ISO-8859-1' ?><REPLY> except the first one in the file and remove all </REPLY> except the last one.

Or is there any other way to create the csv or xlx for record.

1
  • Your problem seems to be here: each time the script runs it appends the same response with separate values with duplicate xml It might be a good idea to share that part of the script at least, to add it to your question.CommentedMay 3, 2020 at 18:40

1 Answer 1

0

With GNU sed, it will start as simple as

sed 's/...//g2' file 

In GNU sed, asking for a general (g) substitution followed by a number means that all the repetitions starting with the number will be replaced.

sed 's@<?xml version='1.0' encoding='ISO-8859-1' ?><REPLY>@@g2' file 

Of course that doesn't change the </REPLY> tags, this would:

sed 's@</REPLY>@@g' file; echo '</REPLY>' >> file 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.