So, I have a file that looks like this:
random stuff in the beginning...
<component>
<name>bob</name>
<age>7</age>
<country>Great_Britain</country>
</component>
<component>
<name>bob_secondbob</name>
<age>7</age>
<country>Great_Britain</country>
</component>
<component>
<name>bam</name>
<age>7</age>
<country>Great_Britain</country>
</component>
etc...
And, I want to just have it as a CSV like this:
name,age,country
bob,7,Great_Britain
bob_secondbob,7,Great_Britain
bam,7,Great_Britain
So I was wondering how would I go about doing this?
So my current code has the word like bob and bam, so i have been greping for it like this and using sed to just get the values:
grep -A4 "<component>" $file | grep -A4 "<name>$bob.*</name>" | grep "<name>"
grep -A4 "<component>" $file | grep -A4 "<name>$bob.*</name>" | grep "<age>"
grep -A4 "<component>" $file | grep -A4 "<name>$bob.*</name>" | grep "<country>"
etc...
Where the variable $bob is "bob";
But the problem is that there are 2 instances of bob.* and I don't know how to separate them so I can print them out...
I looked at XML modules but this file isn't fully XML so I cant use that...
Like bam would be easy to print out by grepping it like that, but if there are multiple instances of bob.* where I need both of them, idk how to print it out properly because grep would return multiple results.
Any suggestion as to how i might go about this?