LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 05-16-2011, 10:11 AM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 474

Rep: Reputation: 72
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?

Last edited by angel115; 05-16-2011 at 10:13 AM.
 
Old 05-16-2011, 10:26 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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

Last edited by colucix; 05-16-2011 at 10:34 AM.
 
1 members found this post helpful.
Old 05-16-2011, 10:42 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 474

Original Poster
Rep: Reputation: 72
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

Last edited by angel115; 05-16-2011 at 10:43 AM.
 
  


Reply

Tags
regular expression, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed character replacement XXLRay Linux - General 6 04-27-2011 07:35 AM
replacement with sed disruptive Programming 7 08-11-2010 11:36 AM
Sed pattern replacement icyrail Programming 3 10-27-2009 05:33 AM
replacement with sed DeepSeaNautilus Programming 6 10-01-2008 06:48 AM
regular expressions and back references within [] lists Tramontane Programming 1 12-19-2003 01:53 PM


All times are GMT -5. The time now is 10:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration