Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving " in a string variable

Status
Not open for further replies.
Jun 18, 2001
21
US
I am trying to search a text file for a string. I create the search criteria as:

STR="STRING GOES HERE"

when I try to search for this string by

grep $STR $FILE

the quotes are stripped off and the grep is incorrect. If I execute:

grep \"$STRING\" $FILE

the grep seems to be ok, but never executes.

What am I doing wrong.

HP-UX 11.00
 
Hi,
I hope the above is a typo in your post or the answer is you are using the wrong varible.

If the first one you say the variable is

$STR

grep $STR $FILE

and in the second you say the variable is

$STRING

grep \"$STRING\" $FILE



 
Hi,
I think you should use:
grep "$STR" $FILE

When you use
grep \"$STR\" $FILE
grep checks for a string "STRING GOES HERE" (including "), and fails to find it.

mewa
 
Sorry, my typo.

$STR is used throughout. The type is only in this posting, not in my script.

My question remains, how to save a string with it's included " marks so when passed to grep, the entire string is searched?
 
Hi,

This is my script:
#!/usr/bin/sh
xx="This is my string"
grep "$xx" my_file

and this in my_file:
This is my string
and this is not.

The script produces:
This is my string

Isn't it what you want to achieve?

mewa
 
Thanks for the help.

"$STR" worked. I tried all inds of meta characters around the " marks, but never actually tried the quote marks themselves.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top