开发者

change last two digits of a number using sed

开发者 https://www.devze.com 2023-02-17 09:10 出处:网络
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).

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).

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).

How can I edit my sed-command to alwa开发者_运维知识库ys change the last two digits to 01 in such cases (I mean only if they are 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.


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.

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.

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号