LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   One sed command covers 3 patterns ? (https://www.linuxquestions.org/questions/linux-general-1/one-sed-command-covers-3-patterns-4175627050/)

thesunlover 04-06-2018 12:54 PM

TBOne,

You are the man with near 20K posts :-) Sorry this one seems not working:

Quote:

sed -i -e '/^johnsmith[[:punct:]]$/d' -e '/^johnsmith$/d'

thesunlover 04-06-2018 01:02 PM

I've tested the following possible cases:

case ,
case ,
case
case[space][space][space]
case whatever
case,
case,[space][space][space]
case,whatever
case, whatever
case;
case:
case1
case2
case3

The following code can remove everything except "site whatever", excluding case1/case2/case3 of course.

Quote:

sed -i /^$username[[:space:]]*$/d $file && sed -i /^$username[[:space:]]*[[:punct:]]/d $file
I believe there must be shorter or better lines of code for this.

Thanks.

thesunlover 04-06-2018 01:10 PM

Studying it now. Just missed it...

Quote:

Originally Posted by MadeInGermany (Post 5840092)
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


MadeInGermany 04-06-2018 01:36 PM

Your latest requirement has the space again.
It can be included in the [ ] character set like this
Code:

[[:space:][:punct:]]
or if you just want , and : instead of all punctuation characters
Code:

[[:space:],:]

TB0ne 04-06-2018 02:56 PM

Quote:

Originally Posted by thesunlover (Post 5840093)
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.

thesunlover 04-06-2018 02:57 PM

MadeInGermany, Thank you much again! It looks like this one working very well:

Quote:

sed -i "/^$username\([[:space:][:punct:]].*\)\{0,1\}$/d" $file

thesunlover 04-06-2018 03:01 PM

Thank you, TBOne.

Let's me keep learning the stuff :)

syg00 04-06-2018 04:58 PM

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.


All times are GMT -5. The time now is 04:44 AM.