阅读背景:

使用sed更改数字的最后两位数字

来源:互联网 

I use this command:

我用这个命令:

sed 's/;\([0-9]*\),\([0-9]*\);/;

I use this command:

我用这个命令:

sed 's/;\([0-9]*\),\([0-9]*\);/;\1.\2;/g;s/;\(\r\?\)$/\1/' inputfile

to change huge csv-files to my needs (see delete ';' at the end of each line).

根据我的需要更改巨大的csv文件(请参阅每行末尾的删除';')。

Now it happens that in some csv-files there are "fictive dates" like 20000500 that can't be imported to SQL because of the last two zeros (that are not possible for dates).

现在碰巧在某些csv文件中有像20000500这样的“虚构日期”由于最后两个零(日期不可能)而无法导入到SQL。

How can I edit my sed-command to always change the last two digits to 01 in such cases (I mean only if they are 00)?

在这种情况下,如何编辑我的sed命令以始终将最后两位数字更改为01(我的意思是只有它们是00)?

I tried

sed 's/;\([0-9]*\),\([0-9]*\);/;\1.\2;/g;s/;\([0-9]{6}\)00;/;\101;/g;s/;\(\r\?\)$/\1/' inputfile

but that doesn't work.

但这不起作用。

1 个解决方案

#1


6  

I think {6} is an extended regular expression. So you either have to use sed -r or you change your regexp to s/;\([0-9][0-9][0-9][0-9][0-9][0-9]\)00;/;\101;/g.

我认为{6}是一个扩展的正则表达式。所以你要么必须使用sed -r,要么你的regexp改为s /; \([0-9] [0-9] [0-9] [0-9] [0-9] [0-9] \)00; /; \ 101 /克。

If you want to use extended regular expressions, do:

如果要使用扩展正则表达式,请执行以下操作:

sed -r 's/;([0-9]{6})00;/;\101;/g'

I.e.: you have to remove the backslashes from parens.

即:你必须从parens中删除反斜杠。

Edit: Regarding to Dennis Williamson's comment it's also possible to use regular regexps by escaping the curly braces:

编辑:关于丹尼斯威廉姆森的评论,也可以通过逃避花括号使用常规正则表达式:

sed 's/;\([0-9]\{6\}\)00;/;\101;/g'

.;/g;s/;\(\r\?\)$/

I use this command:

我用这个命令:

sed 's/;\([0-9]*\),\([0-9]*\);/;\1.\2;/g;s/;\(\r\?\)$/\1/' inputfile

to change huge csv-files to my needs (see delete ';' at the end of each line).

根据我的需要更改巨大的csv文件(请参阅每行末尾的删除';')。

Now it happens that in some csv-files there are "fictive dates" like 20000500 that can't be imported to SQL because of the last two zeros (that are not possible for dates).

现在碰巧在某些csv文件中有像20000500这样的“虚构日期”由于最后两个零(日期不可能)而无法导入到SQL。

How can I edit my sed-command to always change the last two digits to 01 in such cases (I mean only if they are 00)?

在这种情况下,如何编辑我的sed命令以始终将最后两位数字更改为01(我的意思是只有它们是00)?

I tried

sed 's/;\([0-9]*\),\([0-9]*\);/;\1.\2;/g;s/;\([0-9]{6}\)00;/;\101;/g;s/;\(\r\?\)$/\1/' inputfile

but that doesn't work.

但这不起作用。

1 个解决方案

#1


6  

I think {6} is an extended regular expression. So you either have to use sed -r or you change your regexp to s/;\([0-9][0-9][0-9][0-9][0-9][0-9]\)00;/;\101;/g.

我认为{6}是一个扩展的正则表达式。所以你要么必须使用sed -r,要么你的regexp改为s /; \([0-9] [0-9] [0-9] [0-9] [0-9] [0-9] \)00; /; \ 101 /克。

If you want to use extended regular expressions, do:

如果要使用扩展正则表达式,请执行以下操作:

sed -r 's/;([0-9]{6})00;/;\101;/g'

I.e.: you have to remove the backslashes from parens.

即:你必须从parens中删除反斜杠。

Edit: Regarding to Dennis Williamson's comment it's also possible to use regular regexps by escaping the curly braces:

编辑:关于丹尼斯威廉姆森的评论,也可以通过逃避花括号使用常规正则表达式:

sed 's/;\([0-9]\{6\}\)00;/;\101;/g'

/' inputfile sed 's/;\([0-9]



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: