-1

Input

<acc_details acct_no="00000" acct_nm="John"/>
<acc_details acct_no="00001" acct_address="109 BIRHN WAY " acct_nm="BARNS WY"/>
<acc_details acct_no="00002" acct_nm="BILL BAR" phne_nm="123456"/>

Expected output

acct_no,acct_address,acct_nm,phne_nm

00000,,John,
00001,109 BIRHN WAY,BARNS WY,
00002,,BILL BAR,123456
  • Do you mean the double COMMAs? There are no "quote" marks in your data strings. – 0xSheepdog Apr 16 at 14:17
  • Input Input <acc_details acct_no="00000" acct_nm="John"/> <acc_details acct_no="00001" acct_address="109 BIRHN WAY " acct_nm="BARNS WY"/> <acc_details acct_no="00002" acct_nm="BILL BAR" phne_nm="123456"/> Output acct_no,acct_address,acct_nm,phne_nm 00000,,John, 00001,109 BIRHN WAY,BARNS WY, 00002,,BILL BAR,123456 – user347468 Apr 16 at 14:18
  • Please edit your question and add the detail as needed within the body itself. THe comments section is good for these back and forth items, but will get missed by many people who only read the main question. It is also helpful to use the markdown language available to format your post to indicate source code, etc. For help with the markdown, click the (?) at the top right of the 'Body' text box frame when editing your question. Thanks! – 0xSheepdog Apr 16 at 14:22
  • he added it, but did not format the xml as code ;-) – pLumo Apr 16 at 14:34
  • Need to get the values in a text file with all the columns as you can see number of columns are fixed but the column will be missing if the data isnt available in a row like acct_address & phne_nm missing in 1st row, phne_num missing in 2nd row, acct_address missing in 3rd row – user347468 Apr 16 at 14:35
2

Fix the xml file by adding a root tag:

<accounts>
<acc_details acct_no="00000" acct_nm="John"/>
<acc_details acct_no="00001" acct_address="109 BIRHN WAY " acct_nm="BARNS WY"/>
<acc_details acct_no="00002" acct_nm="BILL BAR" phne_nm="123456"/>
</accounts>

Then use an xml parser, e.g. xmlstarlet:

{
echo "acct_no,acct_address,acct_nm,phne_nm"
xmlstarlet sel -t \
    -m '//acc_details' \
    -v "concat(@acct_no,',',@acct_address,',',@acct_nm,',',@phne_nm)" -n \
    input_file
}

Output:

acct_no,acct_address,acct_nm,phne_nm
00000,,John,
00001,109 BIRHN WAY ,BARNS WY,
00002,,BILL BAR,123456
  • If xmlstarlet isn't available then how to go about it – user347468 Apr 16 at 15:02
  • Install it :-) How to do that depends on your system, many OS will offer it from their package system e.g., for Ubuntu it's in universe repository, so you can simply install it using apt. – pLumo Apr 16 at 15:07
  • I understand but i have to go through approvals which will take lot of time. I need a quick fix. Thanks for understanding – user347468 Apr 16 at 15:14
  • Have a look at the python solutions (don't only look at the accepted answer, e.g. xmltodict seems pretty easy). – pLumo Apr 16 at 15:17
  • Need script in only shell – user347468 Apr 16 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.