除了字符的文件删除尾随零 [英] Remove trailing zeros in a file except character
本文介绍了除了字符的文件删除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的值在我的文件:
1.5000
0.006
9.0001
104.2500
17.0000
3.5000
纸
我想删除尾随零,以下 AWK
将删除尾随零但字符值打印零:
的awk'{a = $ 0 + 0;如果(A〜/\\./!)A = A.0;打印}'文件
以上的输出 AWK
:
1.5
0.006
9.0001
104.25
17
3.5
0.0
如何修改 AWK
来跳过字符?
的例外输出:
1.5
0.006
9.0001
104.25
17.0
3.5
纸
解决方案
使用 SED
:
SED的/,\\([0-9] * [。] 0 \\?\\)\\(\\(0 * [1-9] \\ + \\)* \\ )0 * / \\ 1 \\ 2 /'输入
这给出了:
1.5
0.006
9.0001
104.25
17.0
3.5
纸
对于多列文件:
的awk'$ 2〜/ ^ [0-9] + [。] [0-9] [1-9] * 0 + $ / {
$ 2 = gensub(/([] 0?([0-9] * [1-9] +)?)0 * $ /,\\\\ 1,G,$ 2)} 1'输入
I have the following values in my file:
1.5000
0.006
9.0001
104.2500
17.0000
3.5000
paper
I want to remove the trailing zero, the following awk
will remove the trailing zeros but print zeros for character value:
awk '{a=$0+0;if (a!~/\./) a=a".0";print a}' file
Output of above awk
:
1.5
0.006
9.0001
104.25
17
3.5
0.0
How to modify the awk
to skip the characters?
Excepted output:
1.5
0.006
9.0001
104.25
17.0
3.5
paper
解决方案
Using sed
:
sed 's/,\([0-9]*[.]0\?\)\(\(0*[1-9]\+\)*\)0*/,\1\2/' input
This gives:
1.5
0.006
9.0001
104.25
17.0
3.5
paper
For a multi-column file:
awk '$2~/^[0-9]+[.][0-9][1-9]*0+$/{
$2=gensub(/([.]0?([0-9]*[1-9]+)?)0*$/, "\\1", "g", $2)}1' input
这篇关于除了字符的文件删除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文