2016-07-17 88 views
0

我有一個文件,其中包含幾個文件名,我想用grep來收集帶有xsd擴展名的所有文件。當我用我的正則表達式模式使用grep時,我得到了正確的結果,其中約有18個。現在我想將這些結果存儲在一個數組中。我用下面的bash代碼:grep結果數組

targets=($(grep -i "AppointmentManagementService[\.]" AppointmentManagementService\?wsdl)) 

然後我打印數組大小:

echo ${#targets[@]} 

其結果是80而不是18,因爲它僅存儲一個結果的一部分到一個數組細胞。我如何確保只有一個結果進入一個數組單元格?

回答

1

結果可能會分裂到多個單元格上,因爲字符(最可能是空格)被解釋爲內部字段分隔符。

嘗試這樣執行它:

IFS=$'\n' targets=($(grep -i "AppointmentManagementService[\.]" AppointmentManagementService\?wsdl))