如何从电子表格中运行一组终端命令?

时间:2016-07-12 02:24:27

标签: excel shell terminal automation

我必须根据电子表格列中的查询/搜索短语运行一组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次迭代。我如何自动化?

2 个答案:

答案 0 :(得分:0)

假设您的意思是Entrez Direct: E-utilities on the UNIX Command Line,那些Unix shell实用程序会读取和写入文本行,而不是Excel电子表格(.xls)文件。有效的.xls文件是由Excel定义的复杂二进制文件。不要将文本数据写入带有“.xls”文件扩展名(... > <directory>/<name>.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。)

答案 1 :(得分:0)

我建议您将查询列保存在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? ...
相关问题