Applescript-对于grep找到的每个项目

Applescript-对于grep找到的每个项目,applescript,applescript-objc,Applescript,Applescript Objc,这段代码显然不起作用,但我正在寻找一个语法变化,使其工作 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找到的每个项目”语句(我不想

这段代码显然不起作用,但我正在寻找一个语法变化,使其工作

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找到的每个项目”语句(我不想要这个输出):


您的初始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

您的初始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
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