2017-08-30 289 views
0
{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]} 

我坚持我将如何使用sed删除66719222之后的任何内容,然后在最后添加]}。输出应该Bash脚本如何使用sed替换文本后的数字?

这就是我想要删除的内容:

,{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]} 

]}

更换所以输出

{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"}]} 

感谢,如果有人可以帮助。

+0

如果您不得不使用'sed'而不是正确理解文本格式的工具,请尝试:'sed's /},{.*/]}/'' – John1024

回答

0
sed 's/,{"Id":66719222.*/]}/' filename 

或者如果你愿意牺牲清晰度,使命令一个字符的短:

sed 's/,[^}]*66719222.*/]}/' filename 

或更粗略地:

sed 's/.\{7\}66719222.*/]}/' filename 
0

假设file是你的文件名。

sed 's/\(.*\),{"Id":66719222.*/\1\]}/' file 
0

如果是在bash,为什么不直接使用bash:

str='{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}' 

echo "${x%66719222*}66719222]}"