LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-11-2007, 05:21 PM   #1
InJesus
Member
 
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33

Rep: Reputation: 15
sed RegEx problems


I'm trying to match 0000-00-00 00:00:00 then surround it with quotes using sed

sed -i 's/\(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}\)/"\1"/g' regExTest

I've tried a thousand variations can't seem to get it to work, Even if I simplify it just to see if I can get it going with something like 's/\(\d{4}\)/\1'/g' nothing, i'm baffled.

Thanks,

J
 
Old 01-11-2007, 06:41 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I haven't double-checked your syntax, but if you're using {4} and {2} and the like, you're using extended regular expressions. So you need to use the -r switch in there:

sed -i -r [and so forth]

For more sed details, don't miss:

man sed

Hope this helps.
 
Old 01-11-2007, 06:42 PM   #3
InJesus
Member
 
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33

Original Poster
Rep: Reputation: 15
yes, I've tried the -r, It's still not working.
 
Old 01-12-2007, 07:02 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I'm actually working on two Slackware distributions here: my wife's, and my own. My wife's is Slackware 11; my own is Slackware 9.1. My system has an older version of sed, which doesn't even have the -i and -r switches. My wife's system has both, so I've been using hers to pursue your question.

But I don't have access to my wife's system at this point, so help me out here.

It looks as though the regular expression isn't matching. What does "man sed" on your system say about regular expressions? (Search the whole thing for "reg".)
 
Old 01-12-2007, 09:29 AM   #5
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
The following matches on my system:

Code:
# cat regExTest
string 0000-00-00 00:00:00 string

# sed 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\ [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)/"\1"/g' regExTest
string "0000-00-00 00:00:00" string

# sed 's/\(0\{4\}-0\{2\}-0\{2\}\ 0\{2\}:0\{2\}:0\{2\}\)/"\1"/g' regExTest
string "0000-00-00 00:00:00" string
The former to match any digit, the latter to match exactly zeros.

Last edited by colucix; 01-12-2007 at 09:31 AM.
 
Old 01-12-2007, 12:35 PM   #6
InJesus
Member
 
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33

Original Poster
Rep: Reputation: 15
Hey guys, Thanks for the help

That's pretty much what I had to do, I ended up just going 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\)/"\1"/g' although your way seems much more eligant, it only saves a few charectors. I think i must have been looking at different versons GNU does not like the \d switch for digits. Thanks for the reply guys, linuxquestions.org is good because of it.

GB
J
 
Old 01-12-2007, 12:48 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi, InJesus.

The qualifier, { minimum, maximum }, will require a different syntax depending on old or new version of sed, and on whether "-r" is specified on new versions.

Here's a short script:
Code:
#!/bin/sh

# @(#) s1       Demonstrate braces differences in sed.

echo
echo " plain braces, basic re"
echo "xxxx" |
sed 's/x{4}/1234/'

echo
echo " escaped braces, basic re"
echo "xxxx" |
sed 's/x\{4\}/1234/'

echo
echo " plain braces, extended re"
echo "xxxx" |
sed -r 's/x{4}/1234/'

echo
echo " escaped braces, extended re"
echo "xxxx" |
sed -r 's/x\{4\}/1234/'
which produces these different results:
Code:
% ./s1

 plain braces, basic re
xxxx

 escaped braces, basic re
1234

 plain braces, extended re
1234

 escaped braces, extended re
xxxx
Basically, the "-r" reverses the specialness of the "{}". I think you'll need the form which uses "\" prior to the "{}" characters on the old version of sed.

Best wishes ... cheers, makyo
 
  


Reply


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM
regex problem with sed ta0kira Programming 7 06-20-2005 01:33 AM
SED has problems with Ram rabby Linux - Software 5 02-28-2005 07:46 PM
sed problems icisyd Linux - Software 2 11-19-2004 07:17 PM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 02:09 PM


All times are GMT -5. The time now is 06:04 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