LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed and back references replacement (http://www.linuxquestions.org/questions/linux-general-1/sed-and-back-references-replacement-881016/)

angel115 05-16-2011 10:11 AM

sed and back references replacement
 
Hello there,

usually I don't have any issue using regex usually, but today.... It just doesn't want to work.

I've a text file (called "data") containing this:
Code:

20110516
and I what to format it this way 16/05/16
so this should do the trick in theory:
Code:

sed -re 's/^(\d{4})(\d{2})(\d{2})/\3\/\2\/\1/g' data
but it still give me this "20110516"

Any idea?

colucix 05-16-2011 10:26 AM

GNU regexp does not support the \d specification, whereas perl regular expressions use it. You can either do
Code:

sed -re 's/^([0-9]{4})([0-9]{2})([0-9]{2})/\3\/\2\/\1/g' data
or use the [:digit:] specification inside a character list. In alternative you might try the date command:
Code:

date -d $(cat data) +%d/%m/%y

angel115 05-16-2011 10:42 AM

Quote:

Code:

sed -re 's/^([0-9]{4})([0-9]{2})([0-9]{2})/\3\/\2\/\1/g' data


You right using [0-9] is working fine.

Thanks,
Angel


All times are GMT -5. The time now is 01:37 AM.