繁体   English   中英

如何删除 linux/unix 文件中特定行中间的逗号

[英]How to remove commas in the middle of a specific line in a file in linux/unix

有人试图在他们的测试描述中提供帮助。 但是,他们在描述中添加了逗号,这样当测试描述输出到日志文件时,结果就会有额外的逗号。 这使得解析结果变得困难,因为结果文件中的逗号数量不同。

我想使用 sed 并进入测试文件以从描述中删除逗号,这样我们就不会再被咬到了,但我不确定正则表达式应该是什么样子,因为我需要保留其他所有内容和只删除逗号。 该行来自 jmeter jmx 文件。

以下是一些示例行:

1 个逗号

HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime inst = green, 12 hr" enabled="true">

2 个逗号

HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime, inst = network, 2 days" enabled="true">

有人可以提示我如何搜索此行并仅删除逗号,同时保持其他所有内容完好无损吗? 提前感谢您能给我的任何帮助。

编辑: jmx 文件中可能还有其他行也包含逗号,所以我不能盲目地说:

sed -i 's/,//g' file.jmx

您可以使用 tr 从给定字符串中删除所有逗号:

s='HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime, inst = network, 2 days" enabled="true">'
tr -d <<< "$s"

或者在所有具有HTTPSamplerProxy文本的行中使用 sed 将其更改为内联:

sed -i.bak '/HTTPSamplerProxy/s/,//g' file

这个awk应该这样做:

awk '/HTTPSamplerProxy/ {gsub(/,/,"")} 1' file

它将搜索与HTTPSamplerProxy的行,然后替换,什么都没有
完成此操作后, 1将打印所有内容。

如果您sed -i 'code'那样将数据写回原始文件,请执行以下操作:

awk '/HTTPSamplerProxy/ {gsub(/,/,"")} 1' file > tmp && mv tmp file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM