如何在 AppleScript 中 select 行
How to select line in AppleScript
我正在尝试弄清楚如何在日志文件中的一长行文本上使用文本项分隔符。
在信息日志中,总有一个我正在搜索的固定短语,它将我引向文本行。例如,我通过搜索 "[Constant]" 到达我想要的行。
我遇到的问题是我无法 select 整行来执行定界符。下面是一个非常基本的日志示例。
qwertyuiop
mnbvcxza
oqeryuiiop
[Constant] 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs
如有任何建议,我们将不胜感激。
到目前为止,我正在使用:
repeat 16 times
key code 124 using (shift down)
end repeat
哪个做得很好,但很笨重。
查找包含特定字符串的文本行的一种简单方法是 shell 命令 grep
。
set theConstant to "Constant"
set theText to "qwertyuiop
mnbvcxza
oqeryuiiop
Constant 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs"
set foundLine to do shell script "echo " & quoted form of theText & " | tr '\r' '\n' | grep " & quoted form of theConstant
tr
部分用 linefeed
(0x0a) 字符替换 return
(0x0d) 字符是必要的,以符合 shell 行分隔符要求。
如果常量包含特殊字符,那就有点复杂了,因为你必须在将字符传递给 shell.
之前对它们进行转义
set theConstant to "\[Constant\]"
set theText to "qwertyuiop
mnbvcxza
oqeryuiiop
[Constant] 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs"
set foundLine to do shell script "echo " & quoted form of theText & " | tr '\r' '\n' | grep " & quoted form of theConstant
如果你想从磁盘上的文件中读取文本,你可以使用这个
set logFile to (path to library folder from user domain as text) & "Logs:myLogFile.log"
set theText to read file logFile as «class utf8»
你的问题令人费解。您要解析 text/log 文件或脚本以使用某些应用程序的 GUI 吗?因为这就是您的代码所建议的...
如果你想解析一个日志文件,这更容易,你可以使用旧的 Unix 工具OSX。您可以像这样从 Applescript 内部使用它们...
set logfile to "/some/path/file.log"
# Quote the string in case it contains spaces... or add single quotes above...
set qlogfile to quoted form of logfile
# Prepare the shell command to run
set cmd to "grep '^\[Constant]' " & qlogfile & " | cut -c 12- | tr '-' '\n'"
# Run it and capture the output
try
set cmdoutput to (do shell script cmd)
on error
# Oh no, command errored. Best we do something there
end try
结果看起来像这样...
tell current application
do shell script "grep '^\[Constant]' '/some/path/file.log' | cut -c 12- | tr '-' '\n'"
--> "1234567890123456
098765432109876
8765432118976543"
end tell
Result:
"1234567890123456
098765432109876
8765432118976543"
所以要分解它 shell 命令是,
grep ... |
将读取文件的内容和 select 所有以 ^
开头且文本 [Constant]
的行并传递它找到的内容 |
到下一个命令
cut
删除从位置 12
到行尾 -
的字符
tr
将任何字符 -
替换为 \n
,这是 unix 中换行符的代码。
您看到的 \
是因为它是从 Applescript 内部执行的。只有在终端内 运行 才需要 on。
如果您想知道一行与另一行的内容,则删除最后一个命令 | tr '-' '\n'
它将 return
Result:
"1234567890123456-098765432109876-8765432118976543"
我正在尝试弄清楚如何在日志文件中的一长行文本上使用文本项分隔符。
在信息日志中,总有一个我正在搜索的固定短语,它将我引向文本行。例如,我通过搜索 "[Constant]" 到达我想要的行。
我遇到的问题是我无法 select 整行来执行定界符。下面是一个非常基本的日志示例。
qwertyuiop
mnbvcxza
oqeryuiiop
[Constant] 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs
如有任何建议,我们将不胜感激。 到目前为止,我正在使用:
repeat 16 times
key code 124 using (shift down)
end repeat
哪个做得很好,但很笨重。
查找包含特定字符串的文本行的一种简单方法是 shell 命令 grep
。
set theConstant to "Constant"
set theText to "qwertyuiop
mnbvcxza
oqeryuiiop
Constant 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs"
set foundLine to do shell script "echo " & quoted form of theText & " | tr '\r' '\n' | grep " & quoted form of theConstant
tr
部分用 linefeed
(0x0a) 字符替换 return
(0x0d) 字符是必要的,以符合 shell 行分隔符要求。
如果常量包含特殊字符,那就有点复杂了,因为你必须在将字符传递给 shell.
之前对它们进行转义set theConstant to "\[Constant\]"
set theText to "qwertyuiop
mnbvcxza
oqeryuiiop
[Constant] 1234567890123456-098765432109876-8765432118976543
odgnsgnsanfadf
joiergjdfmgadfs"
set foundLine to do shell script "echo " & quoted form of theText & " | tr '\r' '\n' | grep " & quoted form of theConstant
如果你想从磁盘上的文件中读取文本,你可以使用这个
set logFile to (path to library folder from user domain as text) & "Logs:myLogFile.log"
set theText to read file logFile as «class utf8»
你的问题令人费解。您要解析 text/log 文件或脚本以使用某些应用程序的 GUI 吗?因为这就是您的代码所建议的...
如果你想解析一个日志文件,这更容易,你可以使用旧的 Unix 工具OSX。您可以像这样从 Applescript 内部使用它们...
set logfile to "/some/path/file.log"
# Quote the string in case it contains spaces... or add single quotes above...
set qlogfile to quoted form of logfile
# Prepare the shell command to run
set cmd to "grep '^\[Constant]' " & qlogfile & " | cut -c 12- | tr '-' '\n'"
# Run it and capture the output
try
set cmdoutput to (do shell script cmd)
on error
# Oh no, command errored. Best we do something there
end try
结果看起来像这样...
tell current application
do shell script "grep '^\[Constant]' '/some/path/file.log' | cut -c 12- | tr '-' '\n'"
--> "1234567890123456
098765432109876
8765432118976543"
end tell
Result:
"1234567890123456
098765432109876
8765432118976543"
所以要分解它 shell 命令是,
grep ... |
将读取文件的内容和 select 所有以^
开头且文本[Constant]
的行并传递它找到的内容|
到下一个命令cut
删除从位置12
到行尾-
的字符tr
将任何字符-
替换为\n
,这是 unix 中换行符的代码。
您看到的 \
是因为它是从 Applescript 内部执行的。只有在终端内 运行 才需要 on。
如果您想知道一行与另一行的内容,则删除最后一个命令 | tr '-' '\n'
它将 return
Result:
"1234567890123456-098765432109876-8765432118976543"