LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 04-06-2018, 12:54 PM   #16
thesunlover
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 2

TBOne,

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

Quote:
sed -i -e '/^johnsmith[[unct:]]$/d' -e '/^johnsmith$/d'
 
Old 04-06-2018, 01:02 PM   #17
thesunlover
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 2
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:]]*[[unct:]]/d $file
I believe there must be shorter or better lines of code for this.

Thanks.
 
Old 04-06-2018, 01:10 PM   #18
thesunlover
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 2
Studying it now. Just missed it...

Quote:
Originally Posted by MadeInGermany View Post
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
 
Old 04-06-2018, 01:36 PM   #19
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,800

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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:],:]
 
1 members found this post helpful.
Old 04-06-2018, 02:56 PM   #20
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,646

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by thesunlover View Post
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.
 
Old 04-06-2018, 02:57 PM   #21
thesunlover
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 2
MadeInGermany, Thank you much again! It looks like this one working very well:

Quote:
sed -i "/^$username\([[:space:][unct:]].*\)\{0,1\}$/d" $file
 
1 members found this post helpful.
Old 04-06-2018, 03:01 PM   #22
thesunlover
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 2
Thank you, TBOne.

Let's me keep learning the stuff
 
Old 04-06-2018, 04:58 PM   #23
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] understanding sed patterns vincix Linux - Newbie 5 01-08-2018 05:19 AM
sed - using variable values as patterns danielbmartin Programming 7 01-23-2012 02:20 PM
[SOLVED] sed - interleaving remembered patterns danielbmartin Programming 4 01-23-2012 09:10 AM
sed replacing patterns with back quoted command rattlesnakejoe Programming 2 11-18-2009 09:12 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration