I want to match on an odd number of characters

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 21:21:23



How do I match patterns like the following:

\345
\\\345
\\\\\345

The point is that the number of excapes must be an odd number.

 
 
 

I want to match on an odd number of characters

Post by Jan Schamper » Sat, 15 Oct 2005 21:28:17



> \\\\\345
> The point is that the number of excapes must be an odd number.

Talking of RE or shell pattern?

J.

--
"Be liberal in what you accept, and conservative in what you send."
- J. B. Postel, master of the net.

 
 
 

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 22:23:55




> > \\\\\345
> > The point is that the number of excapes must be an odd number.
> Talking of RE or shell pattern?

Match inside a gawk program so I guess I am limited to RE
 
 
 

I want to match on an odd number of characters

Post by Steffen Schule » Sat, 15 Oct 2005 22:47:21


The following REGEX in the awk script matches all sequences of '\' from
odd length and then a positive number of digits:

#!/usr/bin/awk -f
/^\\(\\\\)*[[:digit:]]+$/ {
     print

Quote:}

Regards,

Steffen


> How do I match patterns like the following:

> \345
> \\\345
> \\\\\345

> The point is that the number of excapes must be an odd number.

 
 
 

I want to match on an odd number of characters

Post by Jan Schamper » Sat, 15 Oct 2005 22:51:39





>> > \\\\\345
>> > The point is that the number of excapes must be an odd number.
>> Talking of RE or shell pattern?

> Match inside a gawk program so I guess I am limited to RE

BRE: [\]\{N\}
ERE: [\]{N}
N is the number of occurances. Also remember possible shells escapes.

J.

--
"Be liberal in what you accept, and conservative in what you send."
- J. B. Postel, master of the net.

 
 
 

I want to match on an odd number of characters

Post by Chris F.A. Johnso » Sat, 15 Oct 2005 23:09:40



> How do I match patterns like the following:

> \345
> \\\345
> \\\\\345

> The point is that the number of excapes must be an odd number.

    In a POSIX shell, check the length of ${var%${var##*\\}}:

for var in '\345' '\\345' '\\\345' '\\\\345' '\\\\\345'
do
  bs=${var%${var##*\\}}
  case $(( ${#bs} % 2 )) in
      0) printf "EVEN: " ;;
      1) printf " ODD: " ;;
  esac
  printf "%6s: %d\n" "$bs" "${#bs}"
done

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 23:13:34



> The following REGEX in the awk script matches all sequences of '\' from
> odd length and then a positive number of digits:

> #!/usr/bin/awk -f
> /^\\(\\\\)*[[:digit:]]+$/ {
>      print
> }

> Regards,

> Steffen

Thanks for the responses. The above "nearly" does what I want. The
trouble is the "\" could start anywhere and there could be any number
of "\"s and the above expression will match where it can - one place
after a "\" so it would pick lines with an even number of "\" as well.

If I have this file:

$ cat roland.txt
 abc
   \abc
 \\abc
       \\\abc
   \\\\abc
             \\\\\abc

...and I use gawk like this:

$ gawk '/\\(\\\\)*abc/' roland.txt
   \abc
 \\abc
       \\\abc
   \\\\abc
             \\\\\abc

...then I still get the lines with an even number of escapes because
one position on it will be an odd number of escapes.


> > How do I match patterns like the following:

> > \345
> > \\\345
> > \\\\\345

> > The point is that the number of excapes must be an odd number.

 
 
 

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 23:15:59




> > The following REGEX in the awk script matches all sequences of '\' from
> > odd length and then a positive number of digits:

> > #!/usr/bin/awk -f
> > /^\\(\\\\)*[[:digit:]]+$/ {
> >      print
> > }

> > Regards,

> > Steffen

> Thanks for the responses. The above "nearly" does what I want. The
> trouble is the "\" could start anywhere and there could be any number
> of "\"s and the above expression will match where it can - one place
> after a "\" so it would pick lines with an even number of "\" as well.

> If I have this file:

> $ cat roland.txt
>  abc
>    \abc
>  \\abc
>        \\\abc
>    \\\\abc
>              \\\\\abc

> ...and I use gawk like this:

> $ gawk '/\\(\\\\)*abc/' roland.txt
>    \abc
>  \\abc
>        \\\abc
>    \\\\abc
>              \\\\\abc

> ...then I still get the lines with an even number of escapes because
> one position on it will be an odd number of escapes.

But then there is this that looks promising.

$ gawk '/[^\\]\\(\\\\)*abc/' roland.txt
   \abc
       \\\abc  
             \\\\\abc

 
 
 

I want to match on an odd number of characters

Post by Jan Schamper » Sat, 15 Oct 2005 23:14:55



> N is the number of occurances. Also remember possible shells escapes.

Didn't carefully read. Sorry.
I saw you got your answer.
J.

--
"Be liberal in what you accept, and conservative in what you send."
- J. B. Postel, master of the net.

 
 
 

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 23:20:36





> > > The following REGEX in the awk script matches all sequences of '\' from
> > > odd length and then a positive number of digits:

> > > #!/usr/bin/awk -f
> > > /^\\(\\\\)*[[:digit:]]+$/ {
> > >      print
> > > }

> > > Regards,

> > > Steffen

> > Thanks for the responses. The above "nearly" does what I want. The
> > trouble is the "\" could start anywhere and there could be any number
> > of "\"s and the above expression will match where it can - one place
> > after a "\" so it would pick lines with an even number of "\" as well.

> > If I have this file:

> > $ cat roland.txt
> >  abc
> >    \abc
> >  \\abc
> >        \\\abc
> >    \\\\abc
> >              \\\\\abc

> > ...and I use gawk like this:

> > $ gawk '/\\(\\\\)*abc/' roland.txt
> >    \abc
> >  \\abc
> >        \\\abc
> >    \\\\abc
> >              \\\\\abc

> > ...then I still get the lines with an even number of escapes because
> > one position on it will be an odd number of escapes.

> But then there is this that looks promising.

> $ gawk '/[^\\]\\(\\\\)*abc/' roland.txt
>    \abc
>        \\\abc
>              \\\\\abc

...but then if it starts in the first column then it won't match on the
"[^\\]" as the first character.
 
 
 

I want to match on an odd number of characters

Post by RolandR » Sat, 15 Oct 2005 23:25:25






> > > > The following REGEX in the awk script matches all sequences of '\' from
> > > > odd length and then a positive number of digits:

> > > > #!/usr/bin/awk -f
> > > > /^\\(\\\\)*[[:digit:]]+$/ {
> > > >      print
> > > > }

> > > > Regards,

> > > > Steffen

> > > Thanks for the responses. The above "nearly" does what I want. The
> > > trouble is the "\" could start anywhere and there could be any number
> > > of "\"s and the above expression will match where it can - one place
> > > after a "\" so it would pick lines with an even number of "\" as well.

> > > If I have this file:

> > > $ cat roland.txt
> > >  abc
> > >    \abc
> > >  \\abc
> > >        \\\abc
> > >    \\\\abc
> > >              \\\\\abc

> > > ...and I use gawk like this:

> > > $ gawk '/\\(\\\\)*abc/' roland.txt
> > >    \abc
> > >  \\abc
> > >        \\\abc
> > >    \\\\abc
> > >              \\\\\abc

> > > ...then I still get the lines with an even number of escapes because
> > > one position on it will be an odd number of escapes.

> > But then there is this that looks promising.

> > $ gawk '/[^\\]\\(\\\\)*abc/' roland.txt
> >    \abc
> >        \\\abc
> >              \\\\\abc

> ...but then if it starts in the first column then it won't match on the
> "[^\\]" as the first character.

This does it. Great!! Thanks to all.

$ gawk '/[^\\]\\(\\\\)*abc|^\\(\\\\)*abc/' roland.txt
   \abc
       \\\abc  
             \\\\\abc
\\\\\\\abc

 
 
 

I want to match on an odd number of characters

Post by Steffen Schule » Sat, 15 Oct 2005 23:48:06


The following gawk script solves your problem generally:

awk '/(^|[^\\])\\(\\\\)*($|[^\\])/' file

Regards,

Steffen


> How do I match patterns like the following:

> \345
> \\\345
> \\\\\345

> The point is that the number of excapes must be an odd number.

 
 
 

I want to match on an odd number of characters

Post by RolandR » Sun, 16 Oct 2005 01:02:22



> The following gawk script solves your problem generally:

> awk '/(^|[^\\])\\(\\\\)*($|[^\\])/' file

> Regards,

> Steffen

Thanks. That's much better.
 
 
 

I want to match on an odd number of characters

Post by Michael Tosc » Sun, 16 Oct 2005 03:46:21



> The following gawk script solves your problem generally:

> awk '/(^|[^\\])\\(\\\\)*($|[^\\])/' file

> Regards,

> Steffen

How about a simple
awk '/^[^\\]*\\(\\\\)*/' file
?

--

 
 
 

I want to match on an odd number of characters

Post by Michael Tos » Sun, 16 Oct 2005 04:00:02




> > The following gawk script solves your problem generally:

> > awk '/(^|[^\\])\\(\\\\)*($|[^\\])/' file

> > Regards,

> > Steffen

> How about a simple
> awk '/^[^\\]*\\(\\\\)*/' file
> ?

Sorry, this does not work.
The problem is more complicated than I thought.
Now I understand Steffen's brilliant solution.

--
Michael Tosch
IT Specialist
Managed Services Germany
Hewlett-Packard GmbH
Phone: +49 2407 575 313
Mail: michael.tosch:hp.com

 
 
 

1. Howto count the number of characters in a sentence matching my request

Hi
After 10 hours of 'research' I'm asking for help....plz:
I'm writing a script on
"GNU bash, version 3.00.16(2)-release-(i586-mandrake-linux-gnu)"
and I want to be able to count the number of characters in a sentence
matching my request.
For example:
How many "e" are they in this sentence: "Hello I'm french"

The goal:
/usr/bin/wget --mirror --no-host-directories --cut-dirs=$NbreSousRep
$MyWebSite
$NbreSousRep= Number of "/" in URL of MyWebSite - 1

echo coucou | grep "o" | wc -m
doesn't work of course!

2. Single User Mode ?

3. Help! Only odd number characters are printed

4. Mounting BSD & Minix under Linux

5. regexp for nth character matching

6. Reverse engineer of serial protocol or Ericsson SH888

7. Pattern matching for control characters

8. cant start xwindows

9. Matching characters & digits within a word

10. Shell doesn't interpret pattern matching characters in execl().

11. matching two consecutive characters in (n)awk

12. Shell doesn't interpret pattern matching characters in execl

13. Shell doesn't interpret pattern matching characters in execl().