我有一个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字符,我想每次将命令的结果附加到一个新的lime上。
您的sed命令s/$/r/
正在注入它们。千万别这么做!