在sed中搜索和替换regex是无效的。

时间:2022-06-01 18:04:01

I used the following command to replace all dates with another date string in a file using sed.

我使用下面的命令将所有日期替换为使用sed的文件中的另一个日期字符串。

 time sed -rn 's/\d{4}-\d{2}-\d{2}/2018-03-14/gp' date.txt

date.txt,

date.txt,

(245176,1129,'CLEARED',to_date('1996-09-10','YYYY-MM-DD'),35097600,'Y','Y','N',to_date('1996-09-10','YYYY-MM-DD'),'Y',null,1121,null,null,null,null,1435,null,to_date('1996-09-30','YYYY-MM-DD'),null,-1,35097600,null,1117,to_date('1997-03-25','YYYY-MM-DD'),null,null,null,to_date('1997-03-25','YYYY-MM-DD'),-1,-1,to_date('1997-03-25','YYYY-MM-DD'),-1,1117,null,'ARRERGW',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,204,null,null,null,null,null,null,null,null,null,null,null,null,null);

“清除”(245176年,245176年,to_date(' 1996-09-10 ',' YYYY-MM-DD '),35097600,' Y ',' Y ',' N ',to_date(' 1996-09-10 ',' YYYY-MM-DD ')' Y ',null,1121年,空,空,空,空,1435年,null,to_date(' 1996-09-30 ',' YYYY-MM-DD '),null,1,35097600,null,1117年,to_date(' 1997-03-25 ',' YYYY-MM-DD '),空,空,空,to_date(' 1997-03-25 ',' YYYY-MM-DD '),1,1,to_date(' 1997-03-25 ',' YYYY-MM-DD '),1,1117年,null,ARRERGW,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,204年,空,空,空,空,空,空,空,空,怒噢,空,空,空,空);

Pattern = "\d{4}-\d{2}-\d{2}" Replace String = "2018-03-14"

模式= " \ d { 4 } - \ d { 2 } \ d { 2 }”=“2018-03-14”替换字符串

But the above command is not working for me.. What did I wrong??

但是上面的命令对我不起作用。我错了吗? ?

2 个解决方案

#1


2  

Specify range of digits as a character class [0-9]:

将数字的范围指定为一个字符类[0-9]:

sed -En 's/[0-9]{4}-[0-9]{2}-[0-9]{2}/2018-03-14/gp' date.txt

#2


1  

You can also use ssed (super sed) if you want to use perl regex:

如果您想使用perl regex,也可以使用ssed (super sed):

$ echo '2011-03-02' | ssed -Rn 's/\d{4}-\d{2}-\d{2}/2018-03-14/gp'
2018-03-14

It is less portable but more powerful, so you have to chose as always between flexibility or features/performances.

它的可移植性较差,但功能更强大,因此您必须始终在灵活性或特性/性能之间进行选择。

#1


2  

Specify range of digits as a character class [0-9]:

将数字的范围指定为一个字符类[0-9]:

sed -En 's/[0-9]{4}-[0-9]{2}-[0-9]{2}/2018-03-14/gp' date.txt

#2


1  

You can also use ssed (super sed) if you want to use perl regex:

如果您想使用perl regex,也可以使用ssed (super sed):

$ echo '2011-03-02' | ssed -Rn 's/\d{4}-\d{2}-\d{2}/2018-03-14/gp'
2018-03-14

It is less portable but more powerful, so you have to chose as always between flexibility or features/performances.

它的可移植性较差,但功能更强大,因此您必须始终在灵活性或特性/性能之间进行选择。