2012-10-01 108 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