你的初始grep結果將是字符串格式,例如。一個長串。爲了迭代它們,您需要將字符串轉換爲列表,因此我使用「段落」命令。一旦你有了列表格式的初始grep結果,那麼你可以使用重複循環來處理列表中的項目。在處理這些項目時,您需要將這些結果存儲在新列表中,以便在腳本結尾處可以查看結果。像這樣...
set firstWord to "word"
set secondWord to "end"
-- use the ls command and grep to find all the txt documents in your documents folder
set aFolder to (path to documents folder) as text
set grepResults to do shell script "ls " & quoted form of POSIX path of aFolder & " | grep \"txt\""
set grepResultsList to paragraphs of grepResults
-- search the found txt documents for the words
set totalResults to {}
repeat with aResult in grepResultsList
set thisPath to aFolder & aResult
try
set myCommand to paragraphs of (do shell script "grep -w " & firstWord & space & quoted form of POSIX path of thisPath)
set myCommandCount to count of myCommand
set end of totalResults to {thisPath, firstWord, myCommandCount, myCommand}
end try
try
set mySecondCommand to paragraphs of (do shell script "grep -w " & secondWord & space & quoted form of POSIX path of thisPath)
set mySecondCommandCount to count of mySecondCommand
set end of totalResults to {thisPath, secondWord, mySecondCommandCount, mySecondCommand}
end try
end repeat
return totalResults
非常感謝! – evdude100
沒問題,很樂意幫忙。祝你好運。 – regulus6633