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 |
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. |
|
 |
04-09-2008, 06:28 AM
|
#1
|
|
LQ Newbie
Registered: Apr 2008
Posts: 3
Rep:
|
sed search replace
Hi,
I have the following file:
[root@comp tmp]# cat test.txt
skfs user[someuser][skjfks
slfkjasklfa
fasfkjasfsa
I'm running the following command:
# sed -i 's/user\[.*?\]/user\[newuser\]/g' test.txt
the file is not changed
when i run the following
# sed -i 's/user\[.*\]/user\[newuser\]/g' test.txt
the file does change to have content of
# cat test.txt
skfs user[newuser][skjfks
slfkjasklfa
fasfkjasfsa
can anyone explain to me why the .*? didnt work? after all i do want it to be .*?
Thanks
|
|
|
|
04-09-2008, 06:34 AM
|
#2
|
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,052
Rep: 
|
perhaps the problem is within sed's internals... maybe *? is illegal.
have you tried it with grep already?
|
|
|
|
04-09-2008, 06:44 AM
|
#3
|
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189
Rep: 
|
Quote:
Originally Posted by tomerbd1
can anyone explain to me why the .*? didnt work? after all i do want it to be .*?
|
What are you trying to do?
The .* gives you a repetition of any number of any character, including zero. The ? would be to either include or not include the preceding character. So the syntax .*? makes no sense as far as I can tell.
|
|
|
|
04-09-2008, 06:57 AM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by konsolebox
perhaps the problem is within sed's internals... maybe *? is illegal.
have you tried it with grep already?
|
How do you do search and replace with grep??
tomerbd1;
I got it to work using sed -r or by escaping the "?"
eg:
sed 's/a.*\?b/xxx/' oldfile > newfile
matches ab, or a + any number of characters--optional + b
As already noted, the "?" in the above is redundant
Apparently, sed needs one of these options when using "+" (one or more occurences) or "?" (optional). I have not completely tested, however.
|
|
|
|
04-09-2008, 09:35 AM
|
#5
|
|
LQ Newbie
Registered: Apr 2008
Posts: 3
Original Poster
Rep:
|
Quote:
|
The .* gives you a repetition of any number of any character, including zero. The ? would be to either include or not include the preceding character. So the syntax .*? makes no sense as far as I can tell.
|
But please have a look at:
Quote:
* (star) Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all. ".*" matches "def" "ghi" in abc "def" "ghi" jkl
*? (lazy star) Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item. ".*?" matches "def" in abc "def" "ghi" jkl
|
This is taken from http://www.regular-expressions.info/reference.html
This says that .*? has a meeting it means non greedy.
Note also that the same regular expression works fine for me with grep its just not working with sed...
so the following is working fine and as expected for me:
Quote:
VWdebian:/tmp# perl -pi -w -e 's/user\ \[.*?\]/user\ \[stam\]/g;' file.txt
(replace all usernames with ‘stam’)
|
Last edited by tomerbd1; 04-09-2008 at 09:56 AM.
|
|
|
|
04-09-2008, 01:38 PM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Code:
sed -r 's/user\[[^\]+\]/user\[newuser\]/g' test.txt
Cheers,
Tink
|
|
|
1 members found this post helpful.
|
04-09-2008, 08:34 PM
|
#7
|
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189
Rep: 
|
Cool construction, tink. That does it.
Getting back to the question, why doesn't .*? work in sed -- the various tools in the unix/linux toolbox use different extensions and variations of regular expressions. If you look at the O'Reilly book "Unix in a Nutshell", it gives specifics for the different tools: grep, sed, awk, etc. I think that's where I saw a table listing all the various syntax options and whether they were supported in each of the tools.
|
|
|
|
04-09-2008, 08:51 PM
|
#8
|
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189
Rep: 
|
Quote:
Originally Posted by tomerbd1
|
I had a look at this web site. Interesting. But, I think it does somewhat of a disservice, because it implies that regular expressions are the same from one tool or environment to another. It even says "whether that code is written in Perl, PHP, Java, a .NET language or a multitude of other languages." Unfortunately, this is not precisely true. Thus, people asking, "why doesn't this work in sed?"
The example they give right on their lead page doesn't work in standard grep. The braces need to be escaped. So \{2,4\} gives a repetition of 2 to 4 of the preceding character. But simply {2,4} doesn't work.
|
|
|
|
04-09-2008, 09:33 PM
|
#9
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Quote:
Originally Posted by choogendyk
Cool construction, tink. That does it.
Getting back to the question, why doesn't .*? work in sed
|
Because the old posix sed doesn't know about ?, I don't
think, just as it doesn't know about + ... to enable those
use -r (in GNU sed)
Cheers,
Tink
|
|
|
|
04-10-2008, 04:31 AM
|
#10
|
|
LQ Newbie
Registered: Apr 2008
Posts: 3
Original Poster
Rep:
|
Thanks a lot for the replies 
I have learned from it and now i know exactly whats going on 
Thanks
|
|
|
|
| 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 10:44 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
|
|