LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2012, 05:57 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
sed with RegEx


Have: a file such as this...
Code:
H421J224 Walter Philip George Henry
John Louis H76K3 David Roland H9J8 
James H654J3439 Thomas H0J9 Edward
Kenneth Harold HJ Joseph Ronald Jason
Want: a sed which uses a Regular Expression to remove all character strings which fit this description:
H
one or more digits
J
one or more digits
one blank (unless the match is at the end of a line)

So, H421J224 would be eliminated but H76K3 would not.

I've been stumbling around with this...
Code:
sed -r 's/H[:digit:]+J[:digit:]+[:space:]+//g'
... to no avail.

Please advise.

Daniel B. Martin
 
Old 08-06-2012, 06:37 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
first, you need to put character classes in double brackets---eg [[:digit:]]

Assuming that's fixed, then the last one translates to "one or more spaces"---that's not what you say you wanted.

When you say
"one blank (unless the match is at the end of a line)"
do you mean:
"one blank if NOT at the end of line"?
 
1 members found this post helpful.
Old 08-06-2012, 06:44 PM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
First, using the -r option to SED, tells it to use Extended Regex (ERE) not Regex (RE). Using the + modifier as part of an ERE does not require that you escape it.

Code:
sed -r 's/H[0-9]+J[0-9]+ ?//g'    # or if you must use POSIX char sets
sed -r 's/H[[:digit:]]+J[[:digit:]]+[[:space:]]?//g
Using the + modifier as you did will try to match one or more whitespaces at the end of the line. Since there is no whitespace at the end of the line, it will always fails. Use the ? modifier to match zero or more spaces, which will find a match even at the end of the line.

Hope this helps.

Last edited by towheedm; 08-06-2012 at 06:45 PM.
 
1 members found this post helpful.
Old 08-06-2012, 09:36 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by pixellany View Post
When you say
"one blank (unless the match is at the end of a line)"
do you mean:
"one blank if NOT at the end of line"?
Yes. Your wording is better.

Daniel B. Martin
 
Old 08-06-2012, 09:40 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by towheedm View Post
Code:
sed -r 's/H[0-9]+J[0-9]+ ?//g'
I used this and like it because it uses fewer keystrokes and is more readable (IMHO).

Thank you.

Daniel B. Martin
 
  


Reply

Tags
sed regex



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Regex with sed pixiandreas Linux - Newbie 12 05-16-2012 02:30 PM
sed regex say_hi_ravi Programming 3 09-15-2011 02:12 AM
[SOLVED] sed regex schneidz Programming 1 02-28-2011 06:46 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 AM.

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