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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-11-2007, 04:21 PM
|
#1
|
|
Member
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33
Rep:
|
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
|
|
|
|
01-11-2007, 05:41 PM
|
#2
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
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.
|
|
|
|
01-11-2007, 05:42 PM
|
#3
|
|
Member
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33
Original Poster
Rep:
|
yes, I've tried the -r, It's still not working.
|
|
|
|
01-12-2007, 06:02 AM
|
#4
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
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".)
|
|
|
|
01-12-2007, 08:29 AM
|
#5
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
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 08:31 AM.
|
|
|
|
01-12-2007, 11:35 AM
|
#6
|
|
Member
Registered: Oct 2005
Location: MN
Distribution: Slack, Slamd64
Posts: 33
Original Poster
Rep:
|
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
|
|
|
|
01-12-2007, 11:48 AM
|
#7
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:16 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|