2012-10-01 40 views
1

我有一个名为STRING1的字符串,可能包含双引号。KSH:包含双引号的变量

我通过sed回显字符串来拉出puntuation然后发送到数组来计算某些单词。问题是我无法通过双引号将变量回显到sed。

我爬过我们的文件系统寻找使用FTP命令的文件。 我用grep每个文件为 “FTP”

STRING1=`grep -i ftp $filename` 

如果回声$ STRING1这是输出(只是一个例子)

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])" 

然后,我有这样的代码

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

我尝试过双引号$ STRING1 like

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

但这并不奏效。 单个Qoutes,只需将$ STRING1作为字符串发送给sed即可,因此无效。

我还能在这里做什么?

回答

1

问题在于如何设置STRING1开始。如果我理解你正在试图做正确的东西,你需要写:

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])" 

然后,你可以写:

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

我手动我不是创建STRING1。 STRING1由grep创建。 STRING1 ='grep -i ftp $ filename'我怎样才能把“”附近? – nitrobass24

+0

@ nitrobass24:对不起,我不明白你在做什么。你可以编辑你的问题,包括(1)你如何设置'STRING1'和(2)你输入'echo“$ STRING”'时得到的输出? – ruakh

+0

对不起,我编辑了OP – nitrobass24

0

工作对我来说:

/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 
相关问题