从文件中读取逗号分隔的字符串列表,并对 Bash 中的每个字符串执行命令
Read list of comma separated strings from file and execute command on each string in Bash
我在文件中有一个逗号分隔的字符串列表,如下所示:
输入文件 sentences.txt
包含:
'a1', 'b2', 'c3', 'd4'
在运行对上述每个字符串执行脚本run_script.sh
后,每个输入都会获得相应的输出。
输出文件 output.txt
包含:
'A1', 'B1', 'C1', 'D1'
例如,运行在单个字符串上使用脚本 run_script.sh
就像:
bash run_script.sh a1 > prediction.txt
我想遍历文件的每个元素 sentences.txt
运行 脚本 run_script.sh
并将脚本的输出存储在 prediction.txt
.
如何编写一个有效的 bash 脚本来做到这一点?
我尝试按照建议读取文件 here,但没有得到任何输出。
听起来像是家庭作业。 :)
或者 XY Problem...
首先,为什么 c3
变成了 C1
?这只是一个错字吗?
如果是这样,那么简单地说,将每行大写 -
$: declare -u line; while read -r line; do echo "$line"; done < sentences.txt
'A1', 'B2', 'C3', 'D4'
declare -u line
声明变量 line
始终大写其内容。
while read -r line; do
从 STDIN 循环遍历各行,将每一行分配给 line
。
echo "$line"
字面上只是打印(现在大写的)值。
done < sentences.txt
关闭循环并将文件指定为其 STDIN 的源。
更简单(更快),
tr '[[:lower:]]' '[[:upper:]]' < sentences.txt > prediction.txt
但是,如果你应该单独阅读“句子”中的每个“单词”,那么这些输出会更复杂而不仅仅是大写的拼写错误(如果你正在做基因组工作,这很可能......)然后你需要一些更复杂的解析规则。
- 你确定可以没有嵌入逗号吗?
- 逗号后面总是跟着一个(单个?)Space 字符吗?
- 是否所有字段总是 single-quoted?
- 在 字段中是否有嵌入空格?
- 脚本是否期望在其输入中嵌入 single-quotes 和逗号?
等显然,这些事情会使您的解析崩溃。
尽管如此,假设一个最简单的情况,所有这些假设都有效,仍然有一些相当简单的方法来处理它。
while read -ra words; do
for word in "${words[@]}"; do
run_script.sh "$word"
done
done < sentences.txt > prediction.txt
另一方面,如果这些引号和逗号不应该被视为数据的一部分,您可能有一点 更头疼...
还有一些其他细节需要考虑。
bash run_script.sh a1 > prediction.txt
I want loop through each element of the file sentences.txt run the script run_script.sh and store the output of the script in prediction.txt.
每次都会覆盖文件,完成后唯一的内容就是上次操作的输出。至少,您需要 >>
进行追加,以及一些防止换行直到达到 EOL 的逻辑。
你能给我们多一点工作吗?
我在文件中有一个逗号分隔的字符串列表,如下所示:
输入文件 sentences.txt
包含:
'a1', 'b2', 'c3', 'd4'
在运行对上述每个字符串执行脚本run_script.sh
后,每个输入都会获得相应的输出。
输出文件 output.txt
包含:
'A1', 'B1', 'C1', 'D1'
例如,运行在单个字符串上使用脚本 run_script.sh
就像:
bash run_script.sh a1 > prediction.txt
我想遍历文件的每个元素 sentences.txt
运行 脚本 run_script.sh
并将脚本的输出存储在 prediction.txt
.
如何编写一个有效的 bash 脚本来做到这一点?
我尝试按照建议读取文件 here,但没有得到任何输出。
听起来像是家庭作业。 :)
或者 XY Problem...
首先,为什么 c3
变成了 C1
?这只是一个错字吗?
如果是这样,那么简单地说,将每行大写 -
$: declare -u line; while read -r line; do echo "$line"; done < sentences.txt
'A1', 'B2', 'C3', 'D4'
declare -u line
声明变量 line
始终大写其内容。
while read -r line; do
从 STDIN 循环遍历各行,将每一行分配给 line
。
echo "$line"
字面上只是打印(现在大写的)值。
done < sentences.txt
关闭循环并将文件指定为其 STDIN 的源。
更简单(更快),
tr '[[:lower:]]' '[[:upper:]]' < sentences.txt > prediction.txt
但是,如果你应该单独阅读“句子”中的每个“单词”,那么这些输出会更复杂而不仅仅是大写的拼写错误(如果你正在做基因组工作,这很可能......)然后你需要一些更复杂的解析规则。
- 你确定可以没有嵌入逗号吗?
- 逗号后面总是跟着一个(单个?)Space 字符吗?
- 是否所有字段总是 single-quoted?
- 在 字段中是否有嵌入空格?
- 脚本是否期望在其输入中嵌入 single-quotes 和逗号?
等显然,这些事情会使您的解析崩溃。
尽管如此,假设一个最简单的情况,所有这些假设都有效,仍然有一些相当简单的方法来处理它。
while read -ra words; do
for word in "${words[@]}"; do
run_script.sh "$word"
done
done < sentences.txt > prediction.txt
另一方面,如果这些引号和逗号不应该被视为数据的一部分,您可能有一点 更头疼...
还有一些其他细节需要考虑。
bash run_script.sh a1 > prediction.txt
I want loop through each element of the file sentences.txt run the script run_script.sh and store the output of the script in prediction.txt.
每次都会覆盖文件,完成后唯一的内容就是上次操作的输出。至少,您需要 >>
进行追加,以及一些防止换行直到达到 EOL 的逻辑。
你能给我们多一点工作吗?