【问题标题】:How do I run a set of Terminal commands from a spreadsheet?如何从电子表格运行一组终端命令?
【发布时间】:2016-07-12 16:13:53
【问题描述】:

我必须根据电子表格列中的查询/搜索短语运行一组 Entrez Direct 命令才能在终端中运行。如何根据电子表格自动重复运行终端命令?

即,我有这个命令: esearch -db pubmed -query "<query>" | efetch -format docsum | xtract -pattern DocumentSummary -element PubDate | cut -c 1-4 | sort-uniq-count > <directory>/<name>.xls 还有一个电子表格,它在一列中包含<query> 的值,我还可以输入文件的值以将结果发送到。有 138 次迭代。如何实现自动化?

【问题讨论】:

    标签: excel shell terminal automation


    【解决方案1】:

    我建议您将查询列保存在您的 HOME 目录中的文本文件中 - 可能使用复制和粘贴并将其命名为 queries.txt。让我们想象一下它看起来像这样:

    Why do 24-hr gas stations have locks on their doors?
    Why don't they make planes out of the same material as the indestructible black boxes?
    If nothing sticks to Teflon, how do they stick it to the pan?
    

    然后,我会将以下内容保存为您的 HOME 目录中的runQueries

    #!/bin/bash
    while read query; do
        echo Running query $query...
        esearch -db pubmed -query "$query" | efetch -format docsum | xtract -pattern DocumentSummary  ...
    done < queries.txt
    

    然后您可以在终端中运行一次以使脚本可执行:

    chmod +x runQueries
    

    然后你可以多次运行它,每次都使用它:

    ./runQueries
    

    样本输出

    Running query Why do 24-hr gas stations have locks on their doors?...
    esearch -db pubmed -query Why do 24-hr gas stations have locks on their doors? ...
    Running query Why don't they make planes out of the same material as the indestructible black boxes?...
    esearch -db pubmed -query Why don't they make planes out of the same material as the indestructible black boxes? ...
    Running query If nothing sticks to Teflon, how do they stick it to the pan?...
    esearch -db pubmed -query If nothing sticks to Teflon, how do they stick it to the pan? ...
    

    【讨论】:

      【解决方案2】:

      假设您的意思是 Entrez Direct: E-utilities on the UNIX Command Line,那些 Unix shell 实用程序读取和写入文本行,而不是 Excel 电子表格 (.xls) 文件。有效的 .xls 文件是 Excel 定义的复杂二进制文件。不要将文本数据写入文件扩展名为“.xls”的文件 (... &gt; &lt;directory&gt;/&lt;name&gt;.xls)。这只会导致混乱。

      我想您是在问如何在包含 138 个查询字符串的列表上运行模板 shell 脚本。如果这些查询字符串在电子表格中,最简单的方法是将电子表格导出为 .csv 或 .tsv 文件(逗号分隔值或制表符分隔值),然后编写一个小程序来读取该文件,循环这些查询文本行,将查询文本替换为字符串模板,并发出 shell 命令。 Python 将是一个很好的编程语言选择。它可以作为 bash 脚本来完成,但这将更难编写、更难调试并且更受限制。其他不错的语言选择包括 Java 和 Ruby。

      如果您的查询字符串列表位于 Excel 电子表格中,则更复杂的方法是编写 Excel VBA(Visual Basic for Applications)“宏”来执行相同的步骤。它可以使用VBA 的Shell() 命令来调用一个shell 程序。 (如果您在 Windows 上运行,则需要使用 cygwin 或 mingw 或类似的 shell 来运行命令,而不是标准的 DOS shell。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 2019-03-27
        相关资源
        最近更新 更多