替换日期问题/和未终止的错误

时间:2015-02-08 05:09:59

标签: bash sed

我需要一些帮助,也许还需要一些知识。 我试图将.text文档中的所有日期从dd / mm / yyy更改为dd.mm.yyy。我不会撒谎使用sed让我很困惑!你们有人可以帮助我吗?

`#  DAY          SEP    #2Month     SEP     #3year   min2,max4
sed 's/\[0-3]?[0-9\][.\/]\([0-1]*[0-9]\)[-\/.]\([0-9]\{2,4\}\)/\2.\1\3/'`

这是我的错误sed:文件Frank_Alvarado_hw2.sed第5行:unterminated的's'命令。

Presidency ,President ,Wikipedia Entry,Took office ,Left office ,Party         ,Portrait ,Thumbnail,Home State                                                        
1,George  Washington,http://en.wikipedia.org/wiki/George_Washington,30/04/1789,4/03/1797,Independent ,GeorgeWashington.jpg,thmb_GeorgeWashington.jpg,Virginia

2 个答案:

答案 0 :(得分:1)

如果这些转义在sed中混淆,请使用:

sed -r 's~([0-9]{2})/([0-9]{2})/([0-9]{3})~\1.\2.\3~g' file

即。

  1. 使用-r选项扩展正则表达式
  2. 使用~之类的备用正则表达式分隔符,以避免在模式中转义/
  3. PS:在OSX上使用sed -E代替sed -r

答案 1 :(得分:0)

sed 's/\[0-3]?[0-9\][.\/]\([0-1]*[0-9]\)[-\/.]\([0-9]\{2,4\}\)/\2.\1\3/'
       ^     ^^         ^ ^           ^       ^             ^       ^
       |     ||         | |           |       |             |       L 3th group (missing)
       |     ||         | |           |       |             L end of group 2
       |     ||         | |           |       L start of group 2
       |     ||         | |           L stop group 1
       |     ||         | L Start group 1
       |     ||         L class close so any `0123456789\\][./`
       |     |L class open
       |     L 0 or 1 occurence (of `]`)
       L escape the `[` (not a class open) so litteral char

所以主要是在第二种模式中缺少第三组引用