AWK / SED附加到日志文件

时间:2016-09-15 18:42:17

标签: linux bash shell awk sed

我有一个运行的qpid命令,它为我提供了一个漂亮的打印件,如下所示:

{
"value": {
    "application": {
        "connectionState": "Disconnected",
        "connectionTime": "No connection was established",
        "linkAttributes": {
            "ackSettings": {
                "dataAckEnabled": "true",
                "dataAckTimeout": "5000",
                "dataNakRetryLimit": "0",
                "retransmitDelay": "500"
            },
            "keepAliveSettings": {
                "keepAliveAckTimeout": "5000",
                "keepAliveInterval": "30000"
            },
            "logTraffic": "false",
            "port": "9999",
            "role": "server"
        },
        "protocol": "Class"
    }
},
"queueStats": {}

我接受此输出并去除引号和括号,然后使用以下命令将其附加到日志文件中:

command | sed 's/"//g'  | sed 's/{//g' | sed 's/}//g' | sed -e 's/$/\r/g' |awk '!/""/{ORS=(/},/?RS:""); gsub(/[[:blank:]]+/,""); sub(/}$/,"},\n"); print}' |awk '{gsub(/\\n/,"\n")}1' >> app.log

结果如下:

  ^M^M^MendpointApplications:^MApplicationSide:^MconnectionState:Disconnected,^MconnectionTime:Noconnectionwasestablished,^MlinkAttributes:^MackSettings:^MdataAckEnabled:true,^MdataAckTimeout:5000,^MdataNakRetryLimit:0,^MretransmitDelay:500^M,^MkeepAliveSettings:^MkeepAliveAckTimeout:5000,^MkeepAliveInterval:30000^M,^MlogTraffic:false,^Mport:9999,^Mrole:server^M,^Mprotocol:ClassD^M^M,^MqueueStats:^M

每次我想在新行中将输出附加到日志时,如何摆脱^ M字符?

我的预期结果是:

    endpointApplications:ApplicationSide:connectionState:Disconnected,connectionTime:Noconnectionwasestablished,linkAttributes:ackSettings:dataAckEnabled:true,dataAckTimeout:5000,dataNakRetryLimit:0,retransmitDelay:500,keepAliveSettings:keepAliveAckTimeout:5000,keepAliveInterval:30000,logTraffic:false,port:9999,role:server,protocol:ClassD,queueStats:

基本上,与之前相同的结果但没有^ M个字符,我想每次将命令的结果附加到新的石灰上。

1 个答案:

答案 0 :(得分:2)

您的sed命令s/$/\r/正在注入它们。就是不要那样做!