ProgrammingThis 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.
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 want to remove the username, U=root. However, the username can be anything else as well such as Sandy, Mike, Joe, etc. Therefore, I want to remove U=* but NOT the rest. The result should be
You received 2 good specific answers for the problem.
One of the principles I try to get across to students in my classes is that regular expressions are by design greedy. They will match the longest possible string. So if you need to match fewer characters, you need to supply a constraint of some kind. As you saw, that took the form of additional text that followed the .* part of the pattern.
That being said, however, it is easy to forget details like that when we're busy with a million other things ... cheers, makyo (26)
Druuna, I understand your syntax. Makes sense. Thanks!
Spirit receiver, can you explain how yours works? I understand that [[:space:]] means an actual space but what does [^[:space:]]* mean? I thought [^ ]* will match everything EXCEPT a space. So, wouldn't the syntax be like this:
sed 's/ U=*[^[:space:]]//' - Match everything after U= but do not match space.
Obviously, this doesn't work as I've just tried it. Thanks.
About the [^[:space:]]* construct: This will allow anything except (the ^) whitespace.
And yes, you could also write: [^ ]*. But if there's a tab between root and P= instead of a 'real' space, [^ ]* will be greedy and the P= part will also be targeted (wherever the first 'real' space is found).
Druuna, I understand your syntax. Makes sense. Thanks!
Spirit receiver, can you explain how yours works? I understand that [[:space:]] means an actual space but what does [^[:space:]]* mean? I thought [^ ]* will match everything EXCEPT a space. So, wouldn't the syntax be like this:
sed 's/ U=*[^[:space:]]//' - Match everything after U= but do not match space.
Obviously, this doesn't work as I've just tried it. Thanks.
-twantrd
I think you're confusing the shell meta-character * with the regex repetition operator *.
[ match a character class (matches 1 character)
^ not (i.e. a character that doesn't match one of the following)
[:space:] match a space character
] closing outer bracket
* repeated zero or more times.
i.e. match any sequence of characters (including nothing) that do not contain a space (or newline/cr/etc).
Whereas your string '...U=*[^[:space:]]...' matches U followed by any number of = characters, followed by any character except a space.
Bear in mind that in regular expressions, * doesn't match anything on its own (nor does ?, + etc), these are repetition operators which apply to the preceding pattern.
Last edited by soggycornflake; 09-13-2006 at 12:37 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.