shell脚本添加到

时间:2015-06-16 13:37:08

标签: awk sed

我需要通过读取csv文件附加到xml文件

i / p文件模式 test.dat

account,bill,bill seg
12345,12445,121

14456,14467,903

14456,14467,903

我需要将每一行添加到xml文件样本xml文件

<?xml version="1.0" encoding="UTF-8"?>

<BusinessConfiguration
        xmlns="http://www.portal.com/schemas/BusinessConfig"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.portal.com/schemas/BusinessConfig BusinessConfiguration.xsd">

        <!-- Sample input file for pin_bill_accts containing parameters for bill run management -->
        <!-- Modify according to guidelines -->
        <BillRunConfiguration>
                <!-- List of DOMs for this bill run -->
                <DOMList>
                        <DOM>---1</DOM>
                </DOMList>
              <BillSegmentList>
                </BillSegmentList>

                <!-- List of billing segments for this bill run -->
                <BillingList>
                </BillingList>

我需要在标签下的csv中添加前2条记录,如

<BillingList>
                  <Account>12345</Account>
                    <Billinfo>12445</Billinfo>
                </BillingList>
                  <BillingList>
                    <Account>14456</Account>
                   <Billinfo>14467</Billinfo>
                   </BillingList>

我目前有以下代码:

#!/bin/awk -f

NR==FNR{ a[NR]=$0; next; }
/<BillingList>/{
    print;
    gsub("'","",a[++i]);
    n=split(a[i],arr,",");
    if( n!= 3) { next }
    print "\t<Account>",arr[1],"</Account>";
    print "\t<Billinfo>",arr[2],"</Billinfo>";
    next;
}1

但它只替换第一条记录然后停止。 我是unix的新手,所以请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这将为您提供细分

$ awk -F,                                       # use comma as field separator
          'NR>1&&NF{                            # skip first line and blank lines
                print                           # print the following
                   "<BillingList>               
                      <Account>"$1"</Account>    # insert first field
                      <BillInfo>"$2"</BillInfo> # insert second field
                   </BillingList>"
           }' data.txt

如果需要,您可以在正确的位置添加换行符。