LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
Search this Thread
Old 05-16-2011, 11:11 AM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

Rep: Reputation: 75
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 11:13 AM.
 
Old 05-16-2011, 11:26 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 11:34 AM.
 
1 members found this post helpful.
Old 05-16-2011, 11:42 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 494

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


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


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

Main Menu
Advertisement
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