sed 用于删除尾随零 - 正则表达式 - 非贪婪
我有一个文件,其中有几行如下
ABCD|100.19000|90.100|1000.000010|SOMETHING
BCD|10.100|90.1|100.019900|SOMETHING
现在,在对此应用 sed
后,我希望输出如下(将其用于进一步处理),
ABCD|100.19|90.1|1000.00001|SOMETHING
BCD|10.1|90.1|100.0199|SOMETHING
即我希望所有尾随零(|
之前的那些)将从结果中删除。
我尝试了以下操作:(regtest是包含原始数据的文件,如上所示)
cat regtest | sed 's/|\([0-9]*\)\.\([0-9]*\)0*|/|\1\.\2|/g'
没有工作,因为我认为它很贪婪。
cat regtest | sed 's/|\([0-9]*\)\.\([0-9]*\)0|/|\1\.\2|/g'
会工作的。但是,我必须在同一个文件上重复应用这个 sed 命令来逐个删除零。没有道理。
我该怎么办呢?谢谢!
I have a file which has few lines as below
ABCD|100.19000|90.100|1000.000010|SOMETHING
BCD|10.100|90.1|100.019900|SOMETHING
Now, after applying sed
on this, I would like the output to be as below (To use it for further processing)
ABCD|100.19|90.1|1000.00001|SOMETHING
BCD|10.1|90.1|100.0199|SOMETHING
i.e. I would like all the trailing zeros (the ones before the |
) to be removed from the result.
I tried the following: (regtest is the file containing the original data as shown above)
cat regtest | sed 's/|\([0-9]*\)\.\([0-9]*\)0*|/|\1\.\2|/g'
Did not work as I think it's greedy.
cat regtest | sed 's/|\([0-9]*\)\.\([0-9]*\)0|/|\1\.\2|/g'
Will work. But, I will have to apply this sed
command repeatedly on the same file to remove the zeros one after another. Does not make sense.
How can I go about it? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想依赖 | 删除后面没有 . 的零,则要
如果要 删除后面的零。或数字
(注意我使用 00* 而不是 0+ 以避免其他版本中不可用的 GNU sed 独特功能)
编辑:回答仅删除小数点和管道之间的尾随零的评论请求:
If you want to depend on the | following the zeroes to be removed
If you want to remove zeroes not trailed by a . or a digit
(Note I'm using the 00* instead of 0+ to avoid unique features of GNU sed not available in other versions)
Edit: answer to comment request for removing trailing zeroes only between a decimal point and a pipe:
使用 Perl 的 扩展正则表达式
这会删除(一个点和可选的一些数字)和(管道或行尾)。
Using Perl's extended regular expressions
This removes zeroes that occur between (a dot and optionally some digits) and (a pipe or the end of the line).