Reputation: 63
I have a text stream like this
<device nid="05023CA70900" id="1" fblock="-1" type="switch" name="Appliance Home" brand="Google" active="false" energy_lo="427" />
<device nid="0501C1D82300" id="2" fblock="-1" type="switch" name="TELEVISION Home" brand="Google" active="pending" energy_lo="3272" />
from which i would like an output like
05023CA70900@@1@@-1@@switch@@Appliance Home@@Google@@false@@427
0501C1D82300@@2@@-1@@switch@@TELEVISION Home@@Google@@pending@@3272
There are many lines in the input all of which are not writable.
How can we achieve this using awk or sed ?
Upvotes: 0
Views: 908
Reputation: 203522
awk -F\" -v OFS="@@" '/^<device nid=/ { print $2, $4, $6, $8, $10, $12, $14, $16 }' file
or more generally:
awk -F\" '/^<device nid=/ {for (i=2;i<=NF;i+=2) printf "%s%s",(i==2?"":"@@"),$i; print ""}' file
To address your question in your comment: If you could have a tab in front of <device nid
:
awk -F\" '/^\t?<device nid=// ...'
If you meant something else, update your question and provide more representative input.
Upvotes: 0
Reputation: 67221
Its very simple in perl . So why not use perl ?
perl -lne 'push @a,/\"([\S]*)\"/g;print join "@@",@a;undef @a' your_file
Sample tested:
> cat temp
<device nid="05023CA70900" id="1" fblock="-1" type="switch" name="Appliance Home" brand="Google" active="false" energy_lo="427" />
<device nid="0501C1D82300" id="2" fblock="-1" type="switch" name="TELEVISION Home" brand="Google" active="pending" energy_lo="3272" />
> perl -lne 'push @a,/\"([\S]*)\"/g;print join "@@",@a;undef @a' temp
05023CA70900@@1@@-1@@switch@@Google@@false@@427
0501C1D82300@@2@@-1@@switch@@Google@@pending@@3272
>
Upvotes: 1
Reputation: 785156
Following awk should work:
awk -F '"' '$1 == "<device nid=" { printf("%s@@%s@@%s@@%s@@%s@@%s@@%s@@%s\n",
$2, $4, $6, $8, $10, $12, $14, $16)}' file
PS: It is not always best approach to parse XML using awk/sed.
Upvotes: 1