我有一个名为STRING1的字符串,可能包含双引号。
我通过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 (support@bmc.com.)"
然后我有了这段代码
STRING2=`echo $STRING1|sed 's/[^a-zA-Z0-9]/ /g'`
我尝试过双引号$ STRING1,如
STRING2=`echo "$STRING1"|sed 's/[^a-zA-Z0-9]/ /g'`
但这不起作用。 单Qoutes,只需将$ STRING1作为字符串发送给sed ......所以这不起作用。
我还能在这做什么?
答案 0 :(得分: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 (support@bmc.com.)"
然后你可以写:
STRING2=`echo "$STRING1"|sed 's/[^a-zA-Z0-9]/ /g'`
答案 1 :(得分: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