- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Negate matching group in perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 03:39 AM
тАО04-27-2010 03:39 AM
there is a text like that:
start in
start out
start off
start in
start away
start with
...
I like to match lines:
start (in|out)
but don't like to match lines with all other prepositions:
start (off|away|back|with|...)
How can I implement it in perl? The '^' doesn't seem to negate the () grouping and
the following doesn't work:
m/start\s^(in|out)/
I would appreciate your help with it.
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 03:50 AM
тАО04-27-2010 03:50 AM
SolutionTo match 'start (in|out)' you could simply do:
# perl -ne 'm{start\s+(?:in|out)} and print' file
...the (?:) gives grouping-only without capture.
You could also do:
# perl -ne 'm{start\s+(?=in|out)} and print' file
...where the (?=) denotes a positive look-ahead.
And then:
# perl -ne 'm{start\s+(?!in|out)} and print' file
...which is a negative look-ahead which yields what you seek.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 04:15 AM
тАО04-27-2010 04:15 AM
Re: Negate matching group in perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 04:16 AM
тАО04-27-2010 04:16 AM
Re: Negate matching group in perl
(mine is)
in which case grep knows the option -P and expressions are interpreted perl-style
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 04:41 AM
тАО04-27-2010 04:41 AM
Re: Negate matching group in perl
and yet another perl challange. If my file includes "start
Thanks in advance for sharing your expertise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 04:59 AM
тАО04-27-2010 04:59 AM
Re: Negate matching group in perl
In keeping with yesterday's thread:
# perl -nl -0377 -e 'print m{start\s+with\b}?1:0' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 05:41 AM
тАО04-27-2010 05:41 AM
Re: Negate matching group in perl
I think I was not too specific. This is what I need in meta.
if scirpts includes "start
if anyword is "with" then {
return 1
} else {
return 0
}
} else {
return 1
}
The goal is to find files with "start
Your solution returns false even when no "start
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2010 07:05 AM
тАО04-27-2010 07:05 AM
Re: Negate matching group in perl
Per your last comments, you might do:
# perl -ne '$n++ if m{start\s+(?!with\b)};exit 1 if (m{start\s+with\b} and $n)' file;echo $?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2010 06:10 AM
тАО04-30-2010 06:10 AM
Re: Negate matching group in perl
now I isolated my problem to the following:
if [[ $(perl -nl -0377 -e 'print m{start[\s\n]+(?!with\b)}i;' a.txt) -gt 0 ]]; then
print "Start non-with"
else
print "Start with"
fi
The input files are as follows (_ is for space):
1. =======
start with
2. =======
start
with
3. =======
start_
with
4. =======
start
with
======
1st and 2nd input returns "Start with" message. Why 3rd and 4th don't?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2010 06:56 AM
тАО04-30-2010 06:56 AM
Re: Negate matching group in perl
Let's simplify things. You're looking for the sequence "start with" so:
if [[ -z "$(perl -nl -0377 -e 'print m{start\s+with\b}i' ${FILE})" ]]; then
print "Start non-with"
else
print "Start with"
fi
...
Nothing will be returned if no match is found, hence, we will compare (in the shell) for an empty string.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2010 07:03 AM
тАО04-30-2010 07:03 AM
Re: Negate matching group in perl
BTW, are you able to explain why my previous regexp doesn't work with 3rd and 4th input?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2010 08:27 AM
тАО04-30-2010 08:27 AM
Re: Negate matching group in perl
OK, I think I understand where we left off. Try this:
#!/usr/bin/sh
typeset FILE=$1
if [[ $(perl -0777 -nle 'print m{start\s+(?!\s*with\b)}i?1:0' ${FILE}) -gt 0 ]]; then
print "Start non-with"
else
print "Start with"
fi
...I bungled and mislead you when I erroneously used '-0377'. I meant '-0777' and was thinking of something else. This lead to the match errors.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2010 08:44 AM
тАО04-30-2010 08:44 AM
Re: Negate matching group in perl
I'm not thinking clearly today :-(
Use:
if [[ $(perl -0777 -nle 'print m{start(?!\s+with\b)}i?1:0' ${FILE}) -gt 0 ]]; then
...
If this or the immediately prior post of mine helps, assign 0-points to the first one as this is what I should have offered!
Regards!
...JRF...