LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-28-2005, 10:23 AM   #1
whysyn
Member
 
Registered: Jun 2003
Location: Cleveburg, OH
Distribution: mostly Fedora
Posts: 154

Rep: Reputation: 30
sed / regex question


hello,

i can usually hack my way through regular expressions, but this is troubling me.

here is a sample of text i'm working with:
Code:
#testsed.txt
time:4;
time:37;
time:9;
time:108;
this regex does not work to replace the number:
sed -e 's/time:[0-9]+;/time:99;/' testsed.txt

but this does:
sed -e 's/time:[0-9]*;/time:99;/' testsed.txt

why? as far as i knew, '+' means match 1 or more times. '*' means match any number of times, including none. wouldn't using a '*' cause an empty time field to be filled in with the replacement value? i wouldn't want that to happen...

thanks!
 
Old 06-28-2005, 11:09 AM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Add a -r flag since you're using extended regexps:
Code:
 sed -r -e 's/time:[0-9]+;/time:99;/' testsed.txt
or, if you want to use basic regexps, you need to escape the +:
Code:
 sed -e 's/time:[0-9]\+;/time:99;/' testsed.txt

Last edited by Berhanie; 06-28-2005 at 11:11 AM.
 
Old 06-28-2005, 11:19 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Looking at the man page for grep, which has a bit on regular expressions in general, it seems the + character has to be escaped to use it in that context (but * doesn't). It probably works the same for sed.

Try:
sed -e 's/time:[0-9]\+;/time:99;/' testsed.txt

Dave

Last edited by ilikejam; 06-28-2005 at 11:21 AM.
 
Old 06-28-2005, 02:11 PM   #4
whysyn
Member
 
Registered: Jun 2003
Location: Cleveburg, OH
Distribution: mostly Fedora
Posts: 154

Original Poster
Rep: Reputation: 30
works like a charm. thanks, guys!
 
  


Reply



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
regex problem with sed ta0kira Programming 7 06-20-2005 12:33 AM
sed question lazyuser Programming 4 02-11-2005 06:11 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 01:09 PM
grep (possibly regex) question. mwtheobald Linux - Newbie 1 08-17-2002 03:05 PM
perl regex question JustinHoMi Programming 5 03-20-2002 07:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:51 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
Open Source Consulting | Domain Registration