special timestamp format of csv

回眸只為那壹抹淺笑 提交于 2019-12-02 23:35:44

问题


I have a csv timestamp data of format:

8.11.2012 16:15:10
21.11.2012 15:00:54
11.11.2012 0:24:24
8.11.2012 16:06:53
9.11.2012 0:49:37

I want to apply special timestamp format like this without single digit on each:

08_11_2012_16_15_10
21_11_2012_15_00_54
11_11_2012_00_24_24
08_11_2012_16_06_53

I have tried with regex, search and replace, but got this:

8_11_2012_16_15_10
21_11_2012_15_00_54
11_11_2012_0_24_24
8_11_2012_16_06_53

Does anyone have another idea, maybe with shell awk?


回答1:


You could do it in two passes. Find a character or short sequence of characters that never occurs in your data file. I will use =#= here. The first pass is then very similar to what you have already tried but add =#=0 before all the numbers that should be converted into two digit numbers. So the 8.11.2012 16:15:10 is changed to =#=08_=#=011_2012_=#=016_=#=015_=#=010. Second pass would remove the =#= and the unwanted zeros using a regular expression search for =#=0*(\d\d[^\d]) and replace with \0.

If the file only contains dates and times then you might be able to add the leading zeroes into the text before doing the change that you have already tried. A regular expression search for \b(\d)\b and replace with 0\1 would convert any single digit to two digits. Note that the \b(\d)\b will not see _6_ as a single digit as \b searches for word boundaries and _ is considered to be part of a word. Trying to search for ([^\d])(\d)([^\d]) and replace with \10\2\3 does not work well because it may not handle start and end of line or file as wanted, also it would need to be run twice to process 6.5.2013



来源:https://stackoverflow.com/questions/17150126/special-timestamp-format-of-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!