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.
This can be handled by a \( \) group marker. A following quantifier handles the whole group.
Code:
sed -i "/^$username\([,:].*\)\{0,1\}$/d" $file
It becomes easier if we switch the RE type from BRE (basic regular expression) to ERE (extended regular expression, that is also in egrep or grep -E and in awk and in perl and ...)
Code:
sed -r -i "/^$username([,:].*){0,1}$/d"$file
or even shorter
Code:
sed -r -i "/^$username([,:].*)?$/d" $file
Last but not least, nothing speaks against two simple commands, as TB0ne posted already:
Code:
sed -i "/^$username$/d; /^$username[,:].*$/d" file
TBOne,
You are the man with near 20K posts :-) Sorry this one seems not working:
Code:
sed -i -e '/^johnsmith[[:punct:]]$/d' -e '/^johnsmith$/d'
Right...because you are, again, leaving the $ in at the end. As said before, the $ is the EOL (END OF LINE), so it won't match will it?
As Hazel pointed out initially, use a wild card. If you want "johnsmith<any punctuation><any spaces><end of line>" to work, you've been given all the pieces in this thread to accomplish this. Search for multiple spaces, then anything after it until EOL.
And a MUCH better question here is, why does it **HAVE TO BE** a single SED statment, and why can you not sanitize your data first?? Would seem to be much easier to convert everything to a single case, strip out multiple whitespace characters and replace with single spaces, etc.
You will never stop seeing suggestions that make you think "hmm, that's an interesting way of doing that". Regex is like that.
I will add that I never use "*" unless I can do no else. Tosses up too many "corner case" matches that cause too much angst.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.