shell variables with slashes 
Author Message
 shell variables with slashes

How can I print all lines that match a shell variable that contains slashes?

I assume this doesn't work because of the slashes in $foo

foo=`pwd`
awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

(Stupid example, but shows what's the problem).

Is there a way to do this in _awk_? The sed way of doing this doesn't seem
to work...

Thanks all...

Jaakko



Sun, 15 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:

>How can I print all lines that match a shell variable that contains slashes?

>I assume this doesn't work because of the slashes in $foo

>foo=`pwd`
>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

>(Stupid example, but shows what's the problem).

>Is there a way to do this in _awk_? The sed way of doing this doesn't seem
>to work...

WARNING - Part KSH, part AWK solution coming up...

#!/bin/ksh
export foo=`pwd`
gawk '$0 ~ ENVIRON["foo"]' infile

Notes:
        1) In ksh, you could just use $PWD
        2) The value in the environment variable will not undergo slash
           removal, but will be treated as a regular expression - watch out
           for things like \ . * [] () (etc)



Sun, 15 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:

>How can I print all lines that match a shell variable that contains slashes?

>I assume this doesn't work because of the slashes in $foo

>foo=`pwd`
>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

>(Stupid example, but shows what's the problem).

>Is there a way to do this in _awk_? The sed way of doing this doesn't seem
>to work...

One way is to put the slashes inside brackets ( / ==> [/] ):

foo=`pwd | sed 's:/:[/]:g'`
awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

Untested, but should work.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 15 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:



...
>>I assume this doesn't work because of the slashes in $foo

>>foo=`pwd`
>>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs
...
>WARNING - Part KSH, part AWK solution coming up...

>#!/bin/ksh
>export foo=`pwd`
>gawk '$0 ~ ENVIRON["foo"]' infile

>Notes:
>    1) In ksh, you could just use $PWD
>    2) The value in the environment variable will not undergo slash
>       removal, but will be treated as a regular expression - watch
>       out for things like \ . * [] () (etc)

How about

gawk -v pwd=`pwd` 'index($0, pwd)' infile

which avoids the need for the shell variable and doesn't match
directory names as if they're regex's. But does the original poster
want to match just /tmp or also /var/tmp if pwd is /tmp?

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


<snip>

Quote:
>One way is to put the slashes inside brackets ( / ==> [/] ):

>foo=`pwd | sed 's:/:[/]:g'`
>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

>Untested, but should work.

Well, aside from the unbalanced brackets.

Slashes aren't really regex metacharacters but rather constant regex
delimiters. Unless maximum execution speed is absolutely necessary,
don't use foo as a constant regex but as a 'variable' regex.

awk -v foo=`pwd` '$0 ~ foo' allDirs

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


Quote:


><snip>

>>One way is to put the slashes inside brackets ( / ==> [/] ):

>>foo=`pwd | sed 's:/:[/]:g'`
>>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

>>Untested, but should work.

>Well, aside from the unbalanced brackets.

What unbalanced brackets?

echo "path/to/file" | sed 's:/:[/]:g'

produces:

path[/]to[/]file

Am I missing something???

Chuck Demas
Needham, Mass.

Quote:

>Slashes aren't really regex metacharacters but rather constant regex
>delimiters. Unless maximum execution speed is absolutely necessary,
>don't use foo as a constant regex but as a 'variable' regex.

>awk -v foo=`pwd` '$0 ~ foo' allDirs

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:


> > How can I print all lines that match a shell variable that contains
> > slashes?

> > I assume this doesn't work because of the slashes in $foo

> > foo=`pwd`
> > awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

> > (Stupid example, but shows what's the problem).

> > Is there a way to do this in _awk_? The sed way of doing this doesn't
> > seem to work...

> WARNING - Part KSH, part AWK solution coming up...

Thanks for the warning you hypocrite!

Quote:
> #!/bin/ksh
> export foo=`pwd`
> gawk '$0 ~ ENVIRON["foo"]' infile

> Notes:
>    1) In ksh, you could just use $PWD
>    2) The value in the environment variable will not undergo slash
>       removal, but will be treated as a regular expression - watch out
>       for things like \ . * [] () (etc)

WARNING! Correct answer ahead!

Gee, it's too bad awk doesn't have a simple built-in mechanism for
quoting regular expression metacharacters in strings so that they
can be used as regular expression patterns. Perl does:

  CWD=`pwd`
  perl -ne 'BEGIN { $cwd = quotemeta shift } print if $_ =~ $cwd' $CWD allDirs

Or

  perl -ne 'BEGIN { $cwd = shift } print if $_ =~ qr/$cwd/' $CWD allDirs

By the way: the correct answer to your question about awk, Jaakko, is
to use literal string matching instead of regular expression pattern
matching. The index() function won't {*filter*}on regular expression
metacharacters:

  CWD=`pwd`
  awk 'index($0, "'$CWD'")' allDirs

Or

  awk 'index($0, cwd)' cwd=$CWD allDirs

Kenny's ludicrous suggestion to export the environment variable
and use the ENVIRON array just to get the value of the shell variable
into a variable within awk was a malicious attempt to misdirect
you and thereby sabotage your script. He was trying to punish you
for your "off-topic" post to comp.lang.awk. Ignore him.

--
Jim Monty

Tempe, Arizona USA



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


<snip>

Quote:
>>>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs
...
>>Well, aside from the unbalanced brackets.

>What unbalanced brackets?

...

My fault - wrong term used. Make that braces rather than brackets.
There are 2 '{' in your script but only one '}'.

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


<snip>

Quote:
> WARNING! Correct answer ahead!

How humble!

...

Quote:
>By the way: the correct answer to your question about awk, Jaakko, is
>to use literal string matching instead of regular expression pattern
>matching. The index() function won't {*filter*}on regular expression
>metacharacters:

How original!

Also, unnecessary. Your point about awk lacking an easy way to quote
regex metachars is apt. However, slashes (/) aren't regex metachars per
se, they're constant regex delimiters. Strings including slashes not
preceded by backslashes cause no problems when used as _variable_
regex's in awk, i.e., $0 ~ "a/b/c" rather than $0 ~ /a/b/c/.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


...

Quote:
>>>>awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs
>...
>>>Well, aside from the unbalanced brackets.

>>What unbalanced brackets?
>...

>My fault - wrong term used. Make that braces rather than brackets.
>There are 2 '{' in your script but only one '}'.

Well, I'm sure that's just a typo (or a cut-and-paste accident), but what's
really unbalanced about his post is that he didn't do it the obvious way:

        awk "/$foo/" allDirs

Too much Perl programming will do that to ya.

I like the quote which appeared in this NG in the last week, which sums it
up nicely (wish I could remember who wrote it):

        Perl can do everything AWK can do, but not as elegantly.

For me, that really sums it up.



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:

> How can I print all lines that match a shell variable that contains slashes?

Use fgrep:

  CWD=`pwd`
  fgrep $CWD allDirs

Quote:
> I assume this doesn't work because of the slashes in $foo

> foo=`pwd`
> awk '{ if ($0 ~ /'"$foo"'/) {print}' allDirs

> (Stupid example, but shows what's the problem).

> Is there a way to do this in _awk_? The sed way of doing this doesn't seem
> to work...

Yes, there's a way to do this in awk, but awk is not the Right Tool
For The Job; fgrep is.

--
Jim Monty

Tempe, Arizona USA



Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:


> > WARNING! Correct answer ahead!

> How humble!

Am I to learn humility from the awk gestapo?

Quote:
> > By the way: the correct answer to your question about awk, Jaakko, is
> > to use literal string matching instead of regular expression pattern
> > matching. The index() function won't {*filter*}on regular expression
> > metacharacters:

> How original!

And this is an example of YOUR humility? "Na, na! I suggested
index() first! I beat you! Ha, ha!"

Oh, but the way:


    Newsgroups: comp.lang.awk
    Subject: Re: shell variables with slashes
 -> Date: 29 Jun 2000 02:22:24 GMT


    Newsgroups: comp.lang.awk
    Subject: Re: shell variables with slashes
 -> Date: Thu, 29 Jun 2000 02:53:24 GMT

Quote:
> Also, unnecessary. Your point about awk lacking an easy way to quote
> regex metachars is apt. However, slashes (/) aren't regex metachars per
> se, [...]

They're not regular expression metacharacters, period.

Quote:
> [...] they're constant regex delimiters. Strings including slashes not
> preceded by backslashes cause no problems when used as _variable_
> regex's in awk, i.e., $0 ~ "a/b/c" rather than $0 ~ /a/b/c/.

Once again, you quote me out of context and respond in a manner that
demonstrates you didn't READ the article to which you are responding:

Quote:


> > The value in the environment variable will not undergo slash
> > removal, but will be treated as a regular expression - watch out
> > for things like \ . * [] () (etc)

> Gee, it's too bad awk doesn't have a simple built-in mechanism for
> quoting regular expression metacharacters in strings so that they
> can be used as regular expression patterns.

--
Jim Monty

Tempe, Arizona USA


Mon, 16 Dec 2002 03:00:00 GMT  
 shell variables with slashes


<snip>

On Monty's news server (?) - well, very likely on the server to which
Monty posted:

Quote:

>    Newsgroups: comp.lang.awk
>    Subject: Re: shell variables with slashes
> -> Date: 29 Jun 2000 02:22:24 GMT


>    Newsgroups: comp.lang.awk
>    Subject: Re: shell variables with slashes
> -> Date: Thu, 29 Jun 2000 02:53:24 GMT


OK, OK. Mine showed up in Deja before yours, but I posted to Deja.
FWIW, mine also showed up in remarQ before yours even though remarQ
shows the times as above. Your NNTP server doesn't seem to push things
out too quickly. Just FYI, not meaning to be insulting. And FWIW, Deja
does a crappy job with the time component of posting date.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 17 Dec 2002 03:00:00 GMT  
 shell variables with slashes

Quote:


> <snip>

> On Monty's news server (?) - well, very likely on the server to which
> Monty posted:


> >    Newsgroups: comp.lang.awk
> >    Subject: Re: shell variables with slashes
> > -> Date: 29 Jun 2000 02:22:24 GMT


> >    Newsgroups: comp.lang.awk
> >    Subject: Re: shell variables with slashes
> > -> Date: Thu, 29 Jun 2000 02:53:24 GMT

> OK, OK. Mine showed up in Deja before yours, but I posted to Deja.
> FWIW, mine also showed up in remarQ before yours even though remarQ
> shows the times as above. Your NNTP server doesn't seem to push things
> out too quickly. Just FYI, not meaning to be insulting. And FWIW, Deja
> does a crappy job with the time component of posting date.

I accept your apology for having sarcastically yelled "How original!"
at me in public.

--
Jim Monty

Tempe, Arizona USA



Fri, 20 Dec 2002 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. awk variables --> shell variables

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

3. Passing shell variables to nawk snipet

4. Passing command line variables to AWK in shell script

5. setting shell variables with results from awk

6. shell variable

7. NF and shell variables

8. Exporting awk variables to the UNIX shell

9. help for newbie: way to get the value of an awk variable in a shell script

10. how to use a shell variable in awk for pattern matching

11. Shell variables in AWK

12. Quoting Swamp: Saving an awk command in a shell variable

 

 
Powered by phpBB® Forum Software