Grep -F for special character

 
a='CASH$$A'
/usr/xpg4/bin/grep -F "$a" *.txt

It is not able to grep CASH$$A string as it contains special character $$.
I also tried with

 
/usr/xpg4/bin/grep -F '$a' *.txt

but still not working.

I have to assign CASH$$A to a variable and serach that variable..i dont want to search the string CASH$$A directly.

Please help.

I think you may need to put in a backslash before each $ character. It is likely that this is being converted to the process id.

Try with:-

a='CASH\$\$A'
/usr/xpg4/bin/grep -F "$a" *.txt

What output do you get from a simple:-

echo "$a"

Robin

Odd, works perfectly fine on one of my SPARC SunOS 5.10 boxes:

a='CASH$$A'
/usr/xpg4/bin/grep -F "$a" /tmp/*.txt
/tmp/file.txt:CASH$$A

It works for me on Solaris 10:

$ cat /tmp/file
CASH$$A
CASHSSA
$ a='CASH$$A'; /usr/xpg4/bin/grep -F "$a" /tmp/file
CASH$$A

What's actually in the file?

Given that it's in single-quotes, surely it won't be evaluating the $s at all? (Which is also why '$a' won't work)

1 Like

try this

grep -F $A *.txt

Actually i want to search EXEC<space>*CASH$$A.
For that i have to assign CASH$$A to a variable a...

a='CASH$$A'
/usr/xpg4/bin/grep -F "EXEC *$a" *.txt

And as you guys suggested i cannot do a='CASH\$\$A' becos this variable will be used in other part of the program...So is there anyway where i can achieve this.

Thanks in advance.

Remove that -F option if you are using regular expression. It treats each pattern specified as a string instead of a regular expression.

$ cat file
line 1 EXEC CASH$$A
$ a='CASH$$A'
$ /usr/xpg4/bin/grep "EXEC *$a" file
line 1 EXEC CASH$$A

initially i tried with this approach with string value as CASH$$

a='CASH$$'
/usr/xpg4/bin/grep "EXEC *$a" file

Then this is not working... it is tying to replace $$as pid.

Is there any way to do this..I tried a lot of ways..nothing is working.

After the double quoted expansion, the first grep argument will be EXEC *CASH$$ . In the basic regular expression grammar, that first dollar sign will be treated as a literal dollar sign, but the second will be interpreted as an end of line anchor.

You've done a very poor job of specifying your problem.

You initially posed a question which does not accurately reflect the actual situation (omitting the EXEC * ). Attempts to proactively simplify the matter usually complicate the troubleshooting process (often because the person doing the simplifying cannot identify what is and is not relevant). It is always preferable to provide too much information rather than too little.

Eights posts into this thread and we still do not know if you are trying to match a space followed by an asterisk (in which case you can use -F) or any sequence of consecutive spaces (in which case you MUST NOT use -F). Which is it?

Show us some input data and show us the desired output. Tell us which OS and shell you're using as well. After nearly 5 years of membership and over 100 posts, you should know better.

Regards,
Alister

1 Like