Operating System - HP-UX
1833588 Members
4230 Online
110061 Solutions
New Discussion

grep a filename in a file

 
SOLVED
Go to solution

grep a filename in a file

If I want to grep for the characters

$myenv/*.log

in a file a.a and the file a.a has the exact same characters as above, what's the grep command (using csh). Note: $myenv is an environment variable. I tried backslashes, single quotes, double quotes, and combinations.

17 REPLIES 17
Sanjay_6
Honored Contributor

Re: grep a filename in a file

Hi Elliott,

Try the same in quotes "$myenv/*.log"

Hope this helps.

Regds
Jeff Machols
Esteemed Contributor

Re: grep a filename in a file

you may need an addition .

grep "$env.*.log" a.a
harry d brown jr
Honored Contributor

Re: grep a filename in a file

grep "\$myenv\/\*\.log" a.a

or if you want to "evaluate" $myenv before doing the grep then:

grep "$myenv\/\*\.log" a.a


live free or die
harry
Live Free or Die

Re: grep a filename in a file

Sanjay, Jeff, Harry,

None of your suggestions work, however I think Harry is the closest since a backslash is supposed to stop the special meaning of a character.
Mark Greene_1
Honored Contributor

Re: grep a filename in a file

I couldn't tell which way you were meaning, sorry, so here are two solutions depending on what you are after:

1. if you want to do the variable substitution with the contents of myenv, do this:

eval "grep "$myenv/*.log" a.a"


2. if you are looking for the exact string $myenv, do this:

grep "\$myenv\/\*.log" a.a

HTH
--
mark
the future will be a lot like now, only later
harry d brown jr
Honored Contributor

Re: grep a filename in a file

sorry, for csh:

grep '\$myenv\/\*\.log' test


live free or die
harry
Live Free or Die

Re: grep a filename in a file

Markm I'm looking for exact strin and I tried your example 2
----
2. if you are looking for the exact string $myenv, do this:
----
grep "\$myenv\/\*.log" a.a


and it didn't return anything. Could you try it under csh ?
Jeff Machols
Esteemed Contributor

Re: grep a filename in a file

are you trying to evaluate the $myenv, also is the * literally the *, or is that a wildcard?
harry d brown jr
Honored Contributor

Re: grep a filename in a file

Elliot,

replace "test" with "a.a", in my last example.

live free or die
harry
Live Free or Die

Re: grep a filename in a file

Jeff,

Everything is literal.

Harry,

Your second response works fine, however I only gave you 7 points because I want to see if there are any other different suggestions.

If you reply again I'll give you your remaining points once I see everyone gives up.

Thanks
Darrell Allen
Honored Contributor

Re: grep a filename in a file

This will work:

grep "\$myenv/\*.log" *

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Mark Greene_1
Honored Contributor

Re: grep a filename in a file

Elliot,

Harry's last was the correct syntax, I left off the \ before the . in my example sorry. This is what I got to work in csh (did a direct cut-n-paste instead of retyping it):

grep '\$myenv\/\*\.log' a.a

--
mark
the future will be a lot like now, only later
Jeff Machols
Esteemed Contributor

Re: grep a filename in a file

ok, sorry bout that, try this

grep '\$myenv\/\*\.log' a.a
Jeff Machols
Esteemed Contributor

Re: grep a filename in a file

oops, ignore me, just what harry had with the single quotes, same thing worked for me in C - shell

Re: grep a filename in a file

Does anyone have a correct reply other than Harry's correct reply ?
Mark Greene_1
Honored Contributor
Solution

Re: grep a filename in a file

Harry's is the only correct answer. All of the special characters have to be escaped individually as there is no syntax for telling the shell interpreter to treat as non-special all occurances of special characters in a given string--but it would be nice if there was. Maybe like an "uneval" to do the opposite of "eval". :-)

--
mark
the future will be a lot like now, only later
Darrell Allen
Honored Contributor

Re: grep a filename in a file

Yep, I was wrong. In sh it found $myenv*.log but it also found $myenv*-log. Further, you asked for csh.

Darn, I hate it when work interrupts me when I'm tring to answer a forums question!

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)