用awk

时间:2018-04-29 15:26:33

标签: awk

我有一个xml文件

`<private-keys>
    <private-key>
        <name>ssh_host_rsa_key</name>
    </private-key>
    <private-key>
        <name>test_server_pvt_key</name>
        <certificate-chains>
            <certificate-chain>
                <name>server_cert</name>
         <certificate>MIIECTCCAvGgAwIBAgIBCDANBgkqhkiG9w0BAQsFADCBjDELMAkGA1UEBhMCQ1ox
FjAUBgNVBAgMDVNvdXRoIE1vcmF2aWExDTALBgNVBAcMBEJybm8xDzANBgNVBAoM
...
J1t4nk9saeo87kIuNEDfYNdwYZzRfXoGJ5qIJQK+uJJv9noaIhfFowDW/G14Ji5p
Vh/YtvnOPh7aBjOj8jmzk8MqzK+TZgT7GWu48Nd/NaV8g/DNg9hlN047LaNsJly3
NX3+VBlpMnA4rKwl1OnmYSirIVh9RJqNwqe6k/k=</certificate>
            </certificate-chain>
        </certificate-chains>
    </private-key>
</private-keys>`

我正在尝试使用awk或sed来替换多行证书数据<certificate>...multi line data to be replace...</certificate>和另一个多行数据块,结果将类似于:

`<private-keys>
    <private-key>
        <name>ssh_host_rsa_key</name>
    </private-key>
    <private-key>
        <name>test_server_pvt_key</name>
        <certificate-chains>
            <certificate-chain>
                <name>server_cert</name>
     <certificate>MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68
SfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt
6jAWZDzVfopwpJPAzRPxACDftIqFGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4
...
B4f09aeELM2ASIuk8Q/Mx0jQFnm8lzRFXdewgvdPoZW/7VufM9O7dGPOc41cm2Dh
yrTcXx/VmUBb+/fnXVEgCv7gylp/wtdTGHQBQJHR81jFBz0lnLj+gg==</certificate>
            </certificate-chain>
        </certificate-chains>
    </private-key>
</private-keys>`

我没有任何高级正则表达式实用程序,只有linux shell(甚至不是bash)awk和sed是可用的工具。

1 个答案:

答案 0 :(得分:0)

$ cat tst.awk
NR==FNR {
    cert = (NR==1 ? "" : cert ORS) $0
    next
}
sub(/<certificate>.*/,"") {
    beg = $0 "<certificate>"
    inCert = 1
}
inCert {
    if ( sub(/.*<[\/]certificate>/,"") ) {
        end = "</certificate>" $0
        print beg cert end
        inCert = 0
    }
    next
}
{ print }

$ cat repl
MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68
SfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt
6jAWZDzVfopwpJPAzRPxACDftIqFGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4
...
B4f09aeELM2ASIuk8Q/Mx0jQFnm8lzRFXdewgvdPoZW/7VufM9O7dGPOc41cm2Dh
yrTcXx/VmUBb+/fnXVEgCv7gylp/wtdTGHQBQJHR81jFBz0lnLj+gg==

$ awk -f tst.awk repl file
<private-keys>
    <private-key>
        <name>ssh_host_rsa_key</name>
    </private-key>
    <private-key>
        <name>test_server_pvt_key</name>
        <certificate-chains>
            <certificate-chain>
                <name>server_cert</name>
         <certificate>MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68
SfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt
6jAWZDzVfopwpJPAzRPxACDftIqFGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4
...
B4f09aeELM2ASIuk8Q/Mx0jQFnm8lzRFXdewgvdPoZW/7VufM9O7dGPOc41cm2Dh
yrTcXx/VmUBb+/fnXVEgCv7gylp/wtdTGHQBQJHR81jFBz0lnLj+gg==</certificate>
            </certificate-chain>
        </certificate-chains>
    </private-key>
</private-keys>