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

发布于 2024-11-18 00:36:22 字数 393 浏览 2 评论 0原文

这段代码显然行不通,但我正在寻找语法更改以使其工作。

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

以下输出应该是正确的(我想要的):

word
end
word
end

相反,我得到的是因为我没有这个理论上的“对于 grep 找到的每个项目”语句(我不想要这个输出):

word
word
end
end

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 have this theoretical "for every item that grep finds" statement (I don't want this output):

word
word
end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2024-11-25 00:36:22

您的初始 grep 结果将采用字符串格式,例如。一根长绳子。为了迭代它们,您需要将字符串转换为列表,因此我使用“paragraphs”命令。一旦获得列表格式的初始 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文