使用sed将日期格式从DD / MM / YYYY更改为YYYY-MM-DD

时间:2019-02-17 03:21:28

标签: bash date sed format

我在做

sed -e 's|\([0-9][0-9]\)/\([0-2][0-9]\)/\([0-9][0-9][0-9][0-9]\)|\3-\2-\1|g' 

但是当我运行程序时,出现此错误:

  

“ sed:-e表达式#4,字符62:'s'命令的RHS上无效的引用\ 3”

3 个答案:

答案 0 :(得分:3)

如果捕获组未正确转义,通常会发生此错误。

也就是说,捕获组的括号在命令中转义了,所以也许您尝试将其与扩展正则表达式标志(>>> parser.add_argument('-f', '--foo')sed -r)一起使用万一你不需要逃脱。

请注意,为了便于阅读,您可以将字符范围与数字量词结合起来

sed -E

答案 1 :(得分:1)

这是因为有括号,您需要添加\使其可以作为组捕获:

echo 03/11/2018|sed -e 's|\([0-9][0-9]\)/\([0-2][0-9]\)/\([0-9][0-9][0-9][0-9]\)|\3-\2-\1|g'
2018-11-03

在某些sed版本(例如GNU sed)中,您可以添加-E-r开关,然后转义将变为相反的方式:

echo 03/11/2018|sed -E 's|([0-9][0-9])/([0-2][0-9])/([0-9][0-9][0-9][0-9])|\3-\2-\1|g'
2018-11-03

通过deault,您需要使用\(....\)来将事物分组,()将在字面上匹配括号。
但是,使用-E-r开关时,(...)可以捕获组,\(\)则可以原义地匹配parens。
顺便说一下,{}()的情况相同。

答案 2 :(得分:0)

请您尝试以下。

echo "xxx 03/11/2018" |
awk  '
 match($0,/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/){
   if(RSTART!=1){
     val=substr($0,1,RSTART-1)
   }
   val=val substr($0,RSTART+6,4)"/"substr($0,RSTART+3,2)"/"substr($0,RSTART,2)
   print val substr($0,RSTART+RLENGTH)
}'

说明: 现在添加上述代码的说明。

echo "xxx 03/11/2018" |                                                               ##Using echo command for printing string and passing its output into awk command.
awk  '                                                                                ##Starting awk program here.
 match($0,/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/){                            ##Using match function for matching regex 2 digits / 2 digits / 4 digits.
   if(RSTART!=1){                                                                     ##Checking if RSTART is NOT equal to 1 then do following.
     val=substr($0,1,RSTART-1)                                                        ##Creating variable val whose value is substring from 1 to RSTART-1, RSTART and RLENGTH are variables which will be SET once a match of regex is found bymatch function of awk.
   }                                                                                  ##Closing block of if condition here.
   val=val substr($0,RSTART+6,4)"/"substr($0,RSTART+3,2)"/"substr($0,RSTART,2)        ##Creating variable val here as per OPs need YYYY/MM/DD by using substring and its index values changing.
   print val substr($0,RSTART+RLENGTH)                                                ##Printing value of variable val and substring from value of RSTART+RLENGTH to till end of line.
}'                                                                                    ##Closing block of match now.

这也将处理匹配日期之前和之后的文本。像上面的示例一样,我添加了xxx,它将在以下日期之前出现。

xxx 2018/11/03 


或者如果您要与YYYY/MM/DD行一起打印其他行,请尝试执行以下操作。

echo "xxx 03/11/2018" |
awk  '
 match($0,/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/){
   if(RSTART!=1){
     val=substr($0,1,RSTART-1)
   }
   val=val substr($0,RSTART+6,4)"/"substr($0,RSTART+3,2)"/"substr($0,RSTART,2)
   print val substr($0,RSTART+RLENGTH)
   next
}
1 '

注意: :由于我的awk版本较旧,因此我正在使用match($0,/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/,但我相信如果有以下情况,您可以使用match($0,/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)您的awk是新版本。

相关问题