Linux - GeneralThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 .*?
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
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.
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 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’)
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.