regexp to match EXACTLY one occurance - help!

regexp to match EXACTLY one occurance - help!

Post by Sean Ston » Sat, 17 May 1997 04:00:00



I am trying to put together a regexp inside of a grep statement to match
exactly one occurance of a string, but I can't quite get the syntax right. For
example, say I have the following in a file:

/usr/local/bin
/usr/local
/usr
/bin
/sbin/w

In this example, I want to only match the lines that have a single slash (/).
I've tried everything:

(e)grep '/\/{1}/' file
(e)grep "/\/{1}/" file
(e)grep '/{1}' file
(e)grep /\/{1}/ file
(e)grep "\/\{1\}" file

etc. . . .  (note that in the above I tried both grep and egrep in every
case). I have gnu grep on my linux system.

I have a feeling that even if I do get the syntax correctly, which I believe
is the case in the last attempt above, it will still match all the above lines
because the exactly one occurance is on a per pattern basis. In other words:

grep "\/\{1\}" file

would match ./foo/bar
but not ./foo//bar

I instead need something which only matches one occurance of the pattern per
line . . . How would I do this?

TIA

sean

______________________________________________________________
#include "sig.h"
int main(){
        if (feelingWitty) wittySig();
        else standardSig();
        return 0;}
/*                              Sean


*/

 
 
 

regexp to match EXACTLY one occurance - help!

Post by Jeffrey C. De » Sun, 18 May 1997 04:00:00



>I am trying to put together a regexp inside of a grep statement to match
>exactly one occurance of a string, but I can't quite get the syntax right. For
>example, say I have the following in a file:

>In this example, I want to only match the lines that have a single slash (/).
>I've tried everything:

/^[^/]*/[^/]*$/

Match the beginning of the line, followed by zero or of any character
except '/', followed by '/', followed by zero or more of any character
except '/', followed by the end of the line.

--
  Friendship is born at that moment when one person says to another:
  "What!  You, too?  Thought I was the only one."
                                             -C.S. Lewis

 
 
 

regexp to match EXACTLY one occurance - help!

Post by be » Sun, 18 May 1997 04:00:00


save your old file first because this will edit the file for you:
perl -pi -e 's#.*/.*/.*##sg' filename

 
 
 

regexp to match EXACTLY one occurance - help!

Post by be » Sun, 18 May 1997 04:00:00


Sorry, I should have been more explicit in my last e-mail.  Didn't
want to be vague and am unaware of what your going to use it for.
What I posted will work but this will automatically generate backup:
perl -pi.bak -e 's#.*/.*/.*##sg' filename
and this will simply spit result to stdout from which you can pipe it
to whatever:
perl -pei 's#.*/.*/.*##sg' filename

 
 
 

regexp to match EXACTLY one occurance - help!

Post by Andrew Johnso » Sun, 18 May 1997 04:00:00



> I am trying to put together a regexp inside of a grep statement to match
> exactly one occurance of a string, but I can't quite get the syntax right. For
> example, say I have the following in a file:

> /usr/local/bin
> /usr/local
> /usr
> /bin
> /sbin/w

[snip]
> I instead need something which only matches one occurance of the pattern per
> line . . . How would I do this?

I would suggest using perl, but for an egrep line
remember, you don't need to use '/pattern/' just 'pattern'
...and from your examples it seems you want only those
lines with a leading "/" and no further slashes

egrep '^\/[^/]*$' filename

which matches a leading slash  ^\/
followed by  [^/]*  zero or more non-slashes up to the
end of the string $

regards
andrew

 
 
 

regexp to match EXACTLY one occurance - help!

Post by Eli the Bearde » Mon, 19 May 1997 04:00:00



>In this example, I want to only match the lines that have a single slash (/).
>I've tried everything:

grep '^[^/]*/[^/]*$' file

Perl style breakdown:

m:      # start match regexp
  ^     # anchor to start of line
  [^/]  # not a slash
  *     # zero or more times
  /     # a single slash
  [^/]  # not a slash
  *     # zero or more times
  $     # anchor to end of line
 :x;    # end regexp; treat as multiline regexp

Elijah
------
#!/usr/bin/perl -w

s:[^\w\s,](.*):($u=$1,$u=~s;(\w);(($s=ord($1)-97),chr((($s+$j++)%26)+97
));gex,$u):ex){}exit !print;#18/5/97:etb

 
 
 

regexp to match EXACTLY one occurance - help!

Post by John Sava » Tue, 20 May 1997 04:00:00



>I am trying to put together a regexp inside of a grep statement to match
>exactly one occurance of a string, but I can't quite get the syntax right. For
>example, say I have the following in a file:

>/usr/local/bin
>/usr/local
>/usr
>/bin
>/sbin/w

>In this example, I want to only match the lines that have a single slash (/).

grep '^[^/]*/[^/]*$' works as hoped on my msdos port of grep,
but I don't have access to unix at the moment so can't swear
that it works there.

"print lines beginning with zero or more characters other than
slash, followed by one slash and any number of characters
other than slashes."
--
John Savage     koala ~ sydney.dialix.com.au     <-- use this address

 
 
 

regexp to match EXACTLY one occurance - help!

Post by David Weintra » Fri, 30 May 1997 04:00:00



>I am trying to put together a regexp inside of a grep statement to match
>exactly one occurance of a string, but I can't quite get the syntax right. For
>example, say I have the following in a file:

>/usr/local/bin
>/usr/local
>/usr
>/bin
>/sbin/w

>In this example, I want to only match the lines that have a single slash (/).
>I've tried everything:

>(e)grep '/\/{1}/' file
>(e)grep "/\/{1}/" file
>(e)grep '/{1}' file
>(e)grep /\/{1}/ file
>(e)grep "\/\{1\}" file

>etc. . . .  (note that in the above I tried both grep and egrep in every
>case). I have gnu grep on my linux system.

>I have a feeling that even if I do get the syntax correctly, which I believe
>is the case in the last attempt above, it will still match all the above lines
>because the exactly one occurance is on a per pattern basis. In other words:

>grep "\/\{1\}" file

>would match ./foo/bar
>but not ./foo//bar

Try something like this:

    grep "^[^/]*/[^/]$" $myfile

The first "[^/]*" matches a string that does not contain a slash (which
can be zero length). The "^" and the "$" anchor the string to the beginning
and end of the line.

So you're looking for:

a line that starts with a string (which can be zero length) that does not
contain a slash, then contains a slash, then ends with a string that does
not contain a slash.

With out the beginning and ending line anchors, all lines would be selected.

--
David Weintraub                    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Andersen Consulting               _/                                      _/


                               _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
                          *(Pay no attention to the man behind the curtains)

 
 
 

regexp to match EXACTLY one occurance - help!

Post by Zach Bea » Fri, 30 May 1997 04:00:00


[snipped message about matching lines with exactly one slash]

Quote:

>Try something like this:

>    grep "^[^/]*/[^/]$" $myfile

                  ^^^^  

     Shouldn't this be [^/]* as well? Otherwise, it would only match
lines that had a slash as the second to last character...

Zach

--
"[On the Internet] the ethic of public gift is highly esteemed. The
wealth of [...] software made freely available for public use is
evidence of that ethos."  -- Robin Cover

 
 
 

1. sed - Match 2 occurances of "abc"

How can I use sed to find lines containing 2 or more occurances of
"abc"
Note: There may be other text between each occurance.

Here are my attempts which fail:
sed -n /"abc"\{2,\}/p file.txt
sed -n /\(abc.*\)\{2,\}/p file.txt

I realise that I can use "sed -n /abc.*abc/p file.txt" however this
becomes messy when finding many occurances, or for more comples
strings.

Thanks
Nick

2. SLS update: gcc 2.3.3, libc4.2, etc

3. Better pattern match for multiple occurances in sed?

4. removing lilo ??

5. regexp to match both *.wav and *.au?

6. x-client via tcp/ip?

7. Matching N#######... with shell regexp

8. Router(s) for Internet Service Provider

9. what command /script grab line exactly matched a word

10. regexp for nth character matching

11. test for match by regexp

12. regexp: negating a previous match

13. Is there a single sed regexp to match SCCS version numbers?