1

我有一个名为 STRING1 的字符串,其中可能包含双引号。

我通过 sed 回显字符串以提取标点符号,然后发送到数组以计算某些单词。问题是我无法通过双引号将变量回显到 sed。

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

STRING1=`grep -i ftp $filename`

如果你 echo $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'`

但这不起作用。Single Qoutes,只是将 $STRING1 作为字符串发送到 sed ......所以这不起作用。

我还能在这里做什么?

4

2 回答 2

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'`
于 2012-10-01T20:13:52.180 回答
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 
于 2012-10-08T11:08:24.993 回答