SST
SST

Reputation: 2144

Search and replace with regex is not working in sed

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

 time sed -rn 's/\d{4}-\d{2}-\d{2}/2018-03-14/gp' 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);

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

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

Upvotes: 2

Views: 639

Answers (2)

Allan
Allan

Reputation: 12438

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

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

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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

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

Upvotes: 2

Related Questions