LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2012, 12:12 AM   #1
rsmitha
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
Thumbs up effective regular expression for pattern matching


Hello All,

I have been trying to find a proper regular expression to match (rather unmatch) some strings in the files I have.
I have pasted some strings here from a file named "TestFile":
virtual void Handle(const ABC::reportStatusEngaged& ) = 0;
virtual void Handle(const ABC::reportStatusPathOk& ) = 0;
virtual void Handle(const ABC::reportStatusSteeringOverride& ) = 0;
virtual void Handle(const ABC::reportStatusHeadingConverged& ) = 0;
virtual void Handle(const ABC::reportStatusSpeedOK& ) = 0;
virtual void Handle(const ABC::reportStatusHardwareOK& ) = 0;
virtual void Handle(const ABC::reportStatusImplementOk& ) = 0;
virtual void Handle(const ABC::reportStatusDmuOrientationOk& ) = 0;
virtual void Handle(const ABC::reportStatusValveSteering& ) = 0;
virtual void Handle(const ABC::reportStatusValveOnline& ) = 0;
virtual void Handle(const ABC::reportStatusEngageLimits& ) = 0;
virtual void Handle(const ABC::reportStatusElectricSteer& ) = 0;
virtual void Handle(const ABC::reportStatusValveRequiresPowerCycle& ) = 0;
virtual void Handle(const ABC::reportGuidanceDisengageReasonToken& ) = 0;
virtual void Handle(const ABC::reportGuidanceRemoteEngageToken& ) = 0;
virtual void Handle(const ABC::reportPathErrorsToken& ) = 0;

My aim is to find the strings which have no "Token" in them with Token before the "&". When I run the script on this file after the replacement it should not show any matches.
I tried a number of things out of which one is shown below:
sed -ne 's/\(re.*\)[^Token]&/\1Token\&/' TestFile

The trouble is, it seems to skip some strings like:
reportStatusSteeringOverride&
reportStatusHeadingConverged&
The strings containing "OK".

Please help and advise.
 
Old 09-07-2012, 01:30 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Can you post the desired output from that lot, as your description is unclear to me.
 
Old 09-07-2012, 08:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Your current regex says, find lines containing re followed by zero or more characters up until not one of 'T','o','k','e' or 'n' followed by &
Also, as you have used '-n' in your sed, you either need to remove it or put a 'p' at the end of the replacement:
Code:
sed -ne 's/\(re.*\)[^Token]&/\1Token\&/p' TestFile
You can probably see why you are getting errors. Looking at your 2 examples:

reportStatusSteeringOverride& ... This will not be displayed as the 'e' prior to & is part of the characters of Token
reportStatusHeadingConverged& ... This will be displayed as 'd' is not part of Token
 
Old 09-07-2012, 08:54 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
[^Token] means: contains anything but the letters T,o,k,e or n (and does not mean something which does not contain the world Token).

I would suggest you to search for Token& and skip those lines:
sed ' /Token&/n; s/&/Token&/' file
 
Old 09-07-2012, 07:01 PM   #5
rsmitha
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Chris,

The desired output would be to have all those strings mentioned, to have the world "Token"embedded in them before the "&".
For example, the first one: virtual void Handle(const ABC::reportStatusEngaged& ) should become: virtual void Handle(const ABC::reportStatusEngagedToken& )

Hi Grail,
The problem is "converged" is also not being displayed.

Also, could you guys tell me how to use regexp to negative match an entire word?
I tried [^\<Token\<]. But it does not seem to work.

Regards,
rsmitha.
 
Old 09-08-2012, 04:23 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Maybe something like:
Code:
sed -n '/Token&/! s/&/Token&/p' file
If you are using this to edit a file, change the -n to -i and remove the 'p' at the end.
 
1 members found this post helpful.
Old 09-09-2012, 12:10 AM   #7
rsmitha
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Grail,

I think this works !!

Thank you !!
I was initiallly doing something like this with sed.
sed -n '/re.*Token&/!p' $file
It was giving me the right matches but I did not know how to feed this to the replacement command.
Your reply has given me that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
regular expression to pattern strings bigbada Linux - Newbie 8 03-24-2011 09:18 PM
Extract substring matching a regular expression tikit Linux - General 2 02-18-2008 02:47 PM
bourne shell pattern matching or regular expression powah Programming 2 06-30-2006 11:27 AM
regular expression matching linuxmandrake Programming 2 03-16-2006 07:00 AM


All times are GMT -5. The time now is 04:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration