开发者

Applescript- For every item that grep finds

This code obviously won't work but I'm looking for a syntax change to make it work.

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

The following output should be correct (What I want):

word
end
word
end

instead I get because I do not 开发者_JS百科have this theoretical "for every item that grep finds" statement (I don't want this output):

word
word
end
end


Your initial grep results will be in string format eg. one long string. In order to iterate them you will need to turn the string into a list, thus I use the "paragraphs" command. Once you have the initial grep results in list format, then you can use a repeat loop to process the items in the list. When you process the items you will need to store those results in a new list so that at the end of the script you can view the results in total. Something like this...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜