2011-06-30 13 views
1

このコードは明らかに機能しませんが、動作するように構文の変更を探しています。Applescript- grepが見つけたすべての項目について

for every item that grep finds 
    set myCommand to do shell script "grep -w word test.log" 
    set mySecondCommand to do shell script "grep -w end test.log" 
end 

次の出力は、(私が欲しいもの)正しいはずです:代わりに私が声明(私はしません「のgrepが見つけたすべての項目に対して、」この理論を持っていないので、私が得る

word 
end 
word 
end 

この出力が必要です):

word 
word 
end 
end 

答えて

1

最初のgrep結果は、文字列形式になります。 1つの長い文字列。それらを反復するには、文字列をリストにする必要があります。したがって、私は "段落"コマンドを使用します。リスト形式で最初の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 
+0

ありがとうございました! – evdude100

+0

問題ないです。喜んで助けてください。がんばろう。 – regulus6633

関連する問題