【问题标题】:extract string between two strings from text document using AppleScript使用 AppleScript 从文本文档中提取两个字符串之间的字符串
【发布时间】:2016-09-05 02:51:20
【问题描述】:

我对编写代码非常陌生。我一直在寻找在文本文档中查找字符串然后在下一行返回部分字符串的所有方法。理想情况下,最终目标是将这个提取的字符串放入一个 excel 文件中,但我离这一步还很远。我一直在玩很多不同的选择,但我一生都无法让它发挥作用。我觉得我已经很接近了,这让我很生气,因为我无法弄清楚我哪里出了问题。

目标:在不知道该人姓名的情况下,从下面的文本中提取发布该职位的人的姓名。我知道字符串“职位发布者”将立即预置我要查找的名称,并且我知道“·”将立即跟随该名称。在文本文档的其他任何地方都没有出现这些环绕字符串。

I'm running OS X El Capitan
file name for this example is ExtractedTextOutput.txt
file location for this example is "/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt"

到目前为止,我的尝试如下(我的问题是它似乎只是返回整个文本文档,而不仅仅是我正在寻找的名称)

set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
set theFileContents to read theFile

set output to {}
set od to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"
"}

set all_lines to every text item of theFileContents
repeat with the_line in all_lines
if "Job posted by" is not in the_line then
    set output to output & the_line
else
    set AppleScript's text item delimiters to {"Job posted by"}
    set latter_part to last text item of the_line
    set AppleScript's text item delimiters to {" "}
    set last_word to last text item of latter_part
    set output to output & ("$ " & last_word as string)
end if
end repeat

set AppleScript's text item delimiters to {"
"}

set output to output as string
set AppleScript's text item delimiters to od
return output

非常感谢任何和所有帮助和想法。

文件中的示例文本: 2016 年 9 月 2 日 Datadog 大纽约地区应用安全工程师职位 |领英 60 主页简介 职位描述 我的网络工作  搜索人员、工作、公司等...兴趣  高级   商业服务  转到 Lynda.c 应用安全工程师 数据狗 大纽约市地区 发表于 15 天前 93 次浏览 1 位校友在这里工作 在公司网站上申请 我们的使命是为云操作带来理智,我们需要您在我们的平台上构建弹性和安全的应用程序。你会做什么 执行代码和设计审查,贡献代码以提高整个 Datadog 产品的安全性 教育您的工程师同事了解代码和基础设施的安全性 监控生产应用程序的异常活动 优先考虑和跟踪整个公司的应用程序安全问题 帮助改进我们的安全政策和流程 职位发布者 瑞恩·埃尔伯格·第二 Datadog 大纽约地区技术人才招聘负责人 发送邮件

【问题讨论】:

  • 如果您将 Applescript 的文本项分隔符设置为“职位发布者”,那么您的文本将被分成 2 个文本项:全部在“发布者职位”之前和之后。获取第二部分,并将 Applescript 的文本项分隔符设置为“·”:提取第二部分的第一项:这是您要查找的名称。
  • @pbell 感谢您的快速回复!你能建议如何提取第二部分的第一项吗?到目前为止,我似乎只能让它返回简单的“{}”的整个文本文档,这对我来说似乎什么都不是

标签: macos shell applescript extract automator


【解决方案1】:

我只是在确定您的第二个分隔符到底是什么时遇到了一些困难。你的文本示例显示'·',但是当我检查'Elberg'之后和'2nd ...'之前的内容时,我发现了4个字符:代码32(空格),代码194(¬),代码183(∑) , 代码 32(空格)。

在下面的脚本中,我使用了代码 194。当我将您的文本示例剪切/粘贴到文件中时,它会起作用。这是脚本:

set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
-- your separator seems to be code 32 (space), code 194 (¬), code 183 (∑), code 32 (space)
set Separator to ASCII character 194 -- is it correct ?

set theFileContents to read theFile
set myAuthor to ""
set AppleScript's text item delimiters to {"Job posted by "}
if (count of text item of theFileContents) is 2 then
set Part2 to text item 2 of theFileContents -- this part starts just after "Job posted by "
set AppleScript's text item delimiters to {Separator}
set myAuthor to text item 1 of Part2
end if

log "result=//" & myAuthor & "//" -- show the result in variable myAuthor

注意:如果文本不包含“职位发布者”,则 myAuthor 为 ''。

【讨论】:

  • 你说得对。这次真是万分感谢!当我通过 AppleScript 读取函数运行我的文本文件时,它似乎将“·”更改为代码 183 ∑。但是我仍然无法让applescript返回“Ryan Elberg”。当我在上面运行您的脚本时,它可以工作,但 myAuthor 返回“”。我尝试将分隔符设置为“2nd”,代码 194 和代码 183,但都返回相同的结果,“”
  • 在我的实际 txt 文件中,“Ryan Elberg”在“Job Posted by”之后的新行上是否重要,它们不在一个长字符串中,因为它们出现在我上面的示例文本中。在实际文件中,它显示如下:...政策和流程(换行符)由(换行符)Ryan Elberg 发布的职位·第二(换行符)技术人才主管...
  • 这是否意味着作者总是在 ('Job Posted by' + line break) 之后?你的换行符是什么?仅 ascii 13 还是 ascii 13 +ascii 10 ?或者是其他东西。您只需要调整 applescript 分隔符以包含您的值。我只是在 Ryan Elberg 前后添加换行符后再次测试它:没问题。仍然工作正常
  • "这是否意味着作者总是在 ('Job Posted by' + 换行符) 之后?" -- 是的
  • 不知道我的换行符是什么,ascii 13 or ascii 13+sacii 10
【解决方案2】:

您使用AppleScript's text item delimiters 的想法是正确的,但是您尝试提取名称的方式给您带来了麻烦。不过,首先,我将介绍一些您可以做的事情来改进您的脚本:

set all_lines to every text item of theFileContents
repeat with the_line in all_lines
    if "Job posted by" is not in the_line then
    set output to output & the_line
else
    …
end repeat

无需将文件内容分成几行;如果需要,AppleScript 可以对整个段落或更多段落进行操作。

删除这些不必要的步骤(并添加新步骤以使其适用于整个文件)会大大缩小脚本:

set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
set theFileContents to read theFile

set output to {}
set od to AppleScript's text item delimiters

if "Job posted by" is in theFileContents
    set AppleScript's text item delimiters to {"Job posted by"}
    set latter_part to last text item of theFileContents
    set AppleScript's text item delimiters to {" "}
    set last_word to last text item of latter_part
    set output to output & ("$ " & last_word as string)
else
    display alert "Poster of job listing not found"
    set output to theFileContents
end if

set AppleScript's text item delimiters to od
return output

这就是给你错误输出的原因:

set last_word to last text item of latter_part
set output to output & ("$ " & last_word as string)

这是不正确的。这不是您想要的最后一个字;这是文件的最后一句话!要提取职位列表的海报,请将其更改为以下内容:

repeat with theWord in latterPart
    if the first character in theWord is "¬" then exit repeat
    set output to output & theWord
end repeat

由于 AppleScript 奇怪的 Unicode 处理,无论出于何种原因,将名称与其他文本分开的点 (·) 在通过脚本运行时都会转换为“¬∑”。因此,我们改为寻找“¬”。

一些最后的代码修复:

您的一些变量名使用the_snake_case,而其他变量名使用theCamelCase。使用一种或另一种约定通常是个好主意,所以我也修复了它。

我假设您出于某种原因想要在输出中使用美元符号,所以我保留了它。如果您不想要它,只需将 set output to "$ " 替换为 set output to ""

因此,您的最终工作脚本如下所示:

set theFile to "/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt"
set theFileContents to read theFile as text

set output to "$ "
set od to AppleScript's text item delimiters

if "Job posted by" is in theFileContents then
    set AppleScript's text item delimiters to {"Job posted by"}
    set latterPart to last text item of theFileContents
    set AppleScript's text item delimiters to {" "}
    repeat with theWord in latterPart
        if the first character in theWord is "¬" then exit repeat
        set output to output & theWord
    end repeat
else
    display alert "Poster of job listing not found"
    set output to theFileContents
end if

set AppleScript's text item delimiters to od
return output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多