Home > other >  How to convert <>.*</> to *: * using sed/awk?
How to convert <>.*</> to *: * using sed/awk?

Time:11-05

Have next strings:

<key1>value1</key2>
<key2>value2</key2>
...

I need to get next using sed or awk:

key1: value1
key2: value2
...

CodePudding user response:

A quick and dirty solution, totally unreliable, but it might be ok for a one-off task:

sed 's/<\/.*$//; s/<//; s/>/: /' <<XML
<key1>value1</key2>
<key2>value2</key2>
XML
key1: value1
key2: value2

CodePudding user response:

I would GNU AWK following way, let file.txt content be

<key1>value1</key2>
<key2>value2</key2>

then

awk 'BEGIN{FPAT="[^<>] ";OFS=": "}{print $1,$2}' file.txt

output

key1: value1
key2: value2

Explanation: I inform GNU AWK to treat treat 1 or more of anythng but < and > as field. I print content of 1st and 2nd fields joined using OFS which I set to : . Disclaimer: this assumes every tag is placed on its own' line and no text appear outside tag, please test it with your actual data and check if it does output what you needs.

(tested in gawk 4.2.1)

  • Related