nitrobass24
nitrobass24

Reputation: 379

KSH: Variables containing double quotes

I have a string called STRING1 that could contain double quotes.

I am echoing the string through sed to pull out puntuation then sending to array to count certain words. The problem is I cannot echo variables through double quotes to sed.

I am crawling our filesystems looking for files that use FTP commands. I grep each file for "FTP"

STRING1=`grep -i ftp $filename`

If you echo $STRING1 this is the output (just one example)

myserver> echo "Your file `basename $1` is too large to e-mail. You must ftp the file to BMC tech support. \c"
    echo "Then, ftp it to ftp.bmc.com with the user name 'anonymous.' \c"
    echo "When the ftp is successful, notify technical support by phone (800-537-1813) or by e-mail ([email protected].)"

Then I have this code

STRING2=`echo $STRING1|sed 's/[^a-zA-Z0-9]/ /g'`

I have tried double quoting $STRING1 like

STRING2=`echo "$STRING1"|sed 's/[^a-zA-Z0-9]/ /g'`

But that does not work. Single Qoutes, just sends $STRING1 as the string to sed...so that did not work.

What else can I do here?

Upvotes: 1

Views: 1953

Answers (2)

cdarke
cdarke

Reputation: 44364

Works for me:

/home/user1> S1='"double         quotes"'
/home/user1> echo "$S1"
"double         quotes"

/home/user1> S2=`echo "$S1"|sed 's/[^a-zA-Z0-9]/ /g'` 
/home/user1> echo "$S2"
 double         quotes 

Upvotes: 0

ruakh
ruakh

Reputation: 183341

The problem is with how you're setting STRING1 to begin with. If I understand correctly what you're trying to do, you need to write:

STRING1="Your file `basename $1` is too large to e-mail. You must ftp the file to BMC tech support. \c
Then, ftp it to ftp.bmc.com with the user name 'anonymous.' \c
When the ftp is successful, notify technical support by phone (800-537-1813) or by e-mail ([email protected].)"

Then you can write:

STRING2=`echo "$STRING1"|sed 's/[^a-zA-Z0-9]/ /g'`

Upvotes: 1

Related Questions