regex shell script 
Author Message
 regex shell script

Hi,

I am strugling with a (shell) script that uses awk, here is what I would like to do: I want to match a pattern in a certain column from a file. But, that pattern is not static, it should be parsed from the shell into awk. That is where something doesn't work for me. Here are my (not working) solutions:

---
#!/bin/sh

awkmatch() {
 # no results:
 awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1 p=$2
 # also no results:
 awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1

Quote:
}

echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
---

There must be something wrong with the lines above, I have not enough experience to solve the problem, maybe you could help me?

Robert de Bock.



Tue, 19 Apr 2005 17:31:16 GMT  
 regex shell script


Quote:
> Hi,

> I am strugling with a (shell) script that uses awk, here is what I

would like to do: I want to match a pattern in a certain column from a
file. But, that pattern is not static, it should be parsed from the
shell into awk. That is where something doesn't work for me. Here are my
(not working) solutions:
Quote:

> ---
> #!/bin/sh

> awkmatch() {
>  # no results:
>  awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }'
n=$1 p=$2
>  # also no results:
>  awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ )
print $0 }' n=$1
> }

> echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
> ---

> There must be something wrong with the lines above, I have not enough

experience to solve the problem, maybe you could help me?

Quote:

> Robert de Bock.

Try:

    if ($n ~ p) ...

or, possibly,

    if ($n ~ p".*") ...

p is a variable containing a dynamic RE so you don't need the slashes.

HTH
--
Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
To email me direct please use the above address
This post represents the views of the author and does not
necessarily accurately represent the views of BT



Tue, 19 Apr 2005 17:52:37 GMT  
 regex shell script

Quote:

> Hi,

> I am strugling with a (shell) script that uses awk, here is what I would like to do: I want to match a pattern in a certain column from a file. But, that pattern is not static, it should be parsed from the shell into awk. That is where something doesn't work for me. Here are my (not working) solutions:

> ---
> #!/bin/sh

> awkmatch() {
>  # no results:
>  awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1 p=$2
>  # also no results:
>  awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1
> }

> echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
> ---

> There must be something wrong with the lines above, I have not
> enough experience to solve the problem, maybe you could help me?

       awk  'BEGIN { FS = "," ; OFS = "," }
             { if ( $n ~ /'"$2"'.*/ ) print $0}' n=$1

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Tue, 19 Apr 2005 18:00:18 GMT  
 regex shell script
Hi,

Thats it:
---
awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ ".*"p".*" ) print $0 }' n=$1 p=$2
---
(Hm, that simple, it always seems when solved.) Thank you!

Robert de Bock.

On Fri, 1 Nov 2002 09:52:37 -0000

Quote:


> > Hi,

> > I am strugling with a (shell) script that uses awk, here is what I
> would like to do: I want to match a pattern in a certain column from a
> file. But, that pattern is not static, it should be parsed from the
> shell into awk. That is where something doesn't work for me. Here are my
> (not working) solutions:

> > ---
> > #!/bin/sh

> > awkmatch() {
> >  # no results:
> >  awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }'
> n=$1 p=$2
> >  # also no results:
> >  awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ )
> print $0 }' n=$1
> > }

> > echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
> > ---

> > There must be something wrong with the lines above, I have not enough
> experience to solve the problem, maybe you could help me?

> > Robert de Bock.

> Try:

>     if ($n ~ p) ...

> or, possibly,

>     if ($n ~ p".*") ...

> p is a variable containing a dynamic RE so you don't need the slashes.

> HTH
> --
> Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
> To email me direct please use the above address
> This post represents the views of the author and does not
> necessarily accurately represent the views of BT



Tue, 19 Apr 2005 18:00:27 GMT  
 regex shell script

Quote:
> Hi,

> I am strugling with a (shell) script that uses awk, here is what I would like to do: I want to match a pattern in a certain column from a file. But, that pattern is not static, it should be parsed from the shell into awk. That is where something doesn't work for me. Here are my (not working) solutions:

> ---
> #!/bin/sh

> awkmatch() {
> # no results:
> awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1 p=$2
> # also no results:
> awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1
> }

> echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
> ---

> There must be something wrong with the lines above, I have not enough experience to solve the problem, maybe you could help me?

> Robert de Bock.

--
... don't touch the bang bang fruit


Tue, 19 Apr 2005 19:24:37 GMT  
 regex shell script
Hello,


Quote:
> awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ ".*"p".*" ) print $0 }' n=$1 p=$2

maybe I'm missing something but I'd say that ($n ~ p) is enough.

The ~ operator searches for a match _inside_ the string, not whether the
whole string matches the regexp.

In case you wanted the whole string to match the string $n, you'd have
to do this:
                        $n ~ "^"p"$"
or to use match():
                        match($n, p) && RLENGTH == length($n)

HTH,
        Stepan



Fri, 22 Apr 2005 14:55:45 GMT  
 regex shell script

Quote:
> Hello,



> > awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ ".*"p".*" ) print
$0 }' n=$1 p=$2

> maybe I'm missing something but I'd say that ($n ~ p) is enough.

> The ~ operator searches for a match _inside_ the string, not whether
the
> whole string matches the regexp.

In fact for many RE engines $n ~ ".*"p".*" will be very inefficient
because the engine will first attempt to match the .* part by scanning
to the end of the sring to be matched and then will have to backtrack
until it can attempt to match the first character or pattern stored in
the variable p.

OTOH $n ~ p will be more efficient because the engine will scan through
the string until it finds the first character or pattern stored in the
variable p that it can match, without needing to backtrack.

Except where substitution is being contemplated it is generally more
efficient (especially with NFA engines) not to include .* at the start
or end of a regexp.

[...]
--
Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
To email me direct please use the above address
This post represents the views of the author and does not
necessarily accurately represent the views of BT



Fri, 22 Apr 2005 18:07:55 GMT  
 regex shell script

Quote:
> Here are my (not working) solutions:

> ---
> #!/bin/sh

> awkmatch() {
> # no results:
> awk 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1 p=$2
> # also no results:
> awk -v p="$2" 'BEGIN { FS = "," ; OFS = "," } { if ( $n ~ /p.*/ ) print $0 }' n=$1
> }

> echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar
> ---

Both solutions will work if you change the regular expression you are matching.
Use p instead of /p.*/.  The problem is that it is trying to match the letter
'p' (followed by anything) instead of the value of the variable p.

  if ($n ~ p) print $0

As mentioned previously the .* is not required in this case.

Carlos



Fri, 22 Apr 2005 23:11:16 GMT  
 regex shell script

Quote:
> Hi,

> I am strugling with a (shell) script that uses awk, here is what
> I would like to do: I want to match a pattern in a certain column
> from a file. But, that pattern is not static, it should be parsed
> from the shell into awk. That is where something doesn't work for
> me. Here are my (not working) solutions:
<snip>
> echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar

As I have understood, you just want to print the input line if pattern p
is matched in column n.

$ PATTERN=FooBar
$ COL=3
$ echo "Blaaa,Dooo,FooBarExtra" | awk -F, -v n=$COL -v p=$PATTERN '$n ~ p'
Blaaa,Dooo,FooBarExtra

--
Stein Arne



Sun, 24 Apr 2005 19:25:04 GMT  
 regex shell script


Quote:

> > Hi,

> > I am strugling with a (shell) script that uses awk, here is what
> > I would like to do: I want to match a pattern in a certain column
> > from a file. But, that pattern is not static, it should be parsed
> > from the shell into awk. That is where something doesn't work for
> > me. Here are my (not working) solutions:
> <snip>
> > echo "Blaaa,Dooo,FooBarExtra" | awkmatch 3 FooBar

> As I have understood, you just want to print the input line if pattern
p
> is matched in column n.

> $ PATTERN=FooBar
> $ COL=3
> $ echo "Blaaa,Dooo,FooBarExtra" | awk -F, -v n=$COL -v p=$PATTERN '$n
~ p'
> Blaaa,Dooo,FooBarExtra

Strictly you don't need the -v option (which was added to awk to provide
a way of making variable values available in the BEGIN block) in this
case (the BEGIN block doesn't need to use the values).  This works just
as well:

$ echo "Blaaa,Dooo,FooBarExtra" | awk -F, '$n ~ p' n=$COL p=$PATTERN
Blaaa,Dooo,FooBarExtra

$

I agree that there's no need for the if (...) ... inside the outer
action braces or an explicit print $0 when no other work is being
carried out on $0.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Mon, 25 Apr 2005 09:27:19 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Scheme Shell: Shell Scripting for the 90's

2. Scheme Shell: Shell Scripting for the 90's

3. Scheme Shell: Shell Scripting for the 90's

4. Scheme Shell: Shell Scripting for the 90's

5. Scheme Shell: Shell Scripting for the 90's

6. expect script with shell script

7. Set expect script variables via a korn shell script

8. converting korn shell scripts to tcl scripts

9. grep regex to ruby regex...

10. Regex++ or other Regex lib for Fortran?

11. Python regex / libc regex interactions

12. AWK-Script to set a shell variable (Win32) ?

 

 
Powered by phpBB® Forum Software