Delimited by comma using AWK or SED with the tags

2019-03-06 18:03发布

Delimited by comma using AWK or SED with the tags used below:

[BEGIN AccountID]
        [BEGIN CallerID]
            [BEGIN Billed Account Attributes]
            1111111
            1111111
            1111111
            [END Billed Account Attributes]

        [BEGIN OBIO Tax]
        10
        20
        30
        [END OBIO Tax]

        [BEGIN RINO Tax]
        777
        888
        999
        [END RINO Tax]
    [BEGIN CallerID]
[END AccountID]


[BEGIN AccountID]
    [BEGIN CallerID]
        [BEGIN Billed Account Attributes]
        2222222
        2222222
        2222222
        [END Billed Account Attributes]

        [BEGIN OBIO Tax]
        40
        50
        60
        [END OBIO Tax]

    [BEGIN CallerID]
[END AccountID]

I want a AWK or SED script to print this:

1111111,1111111,1111111,  10,20,30,  777,888,999

2222222,2222222,2222222,  40,50,60,    0,  0,  0
....
....
....

Delimited by comma and put ZERO when RINO TAX is not displayed.

Thanks so much!!!!

1条回答
【Aperson】
2楼-- · 2019-03-06 18:28

This might work for you (GNU sed):

sed '/\[BEGIN AccountID\]/,/\[END AccountID\]/!d;/\[BEGIN AccountID\]/{h;d};/./H;/\[END AccountID\]/!d;g;s/\n*\[[^\n]*\n*//g;s/\n/,/g;s/\s*//g;ta;:a;s/,//9;t;s/$/0,0,0/' file

N.B. This removes spaces and blank lines.

查看更多
登录 后发表回答