读取bash中的多个文件 [英] read multiple files in bash

查看:46
本文介绍了读取bash中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个.txt文件,我想同时在.sh脚本中逐行读取一行.这两个.txt文件的行数相同.在循环内部,我想使用sed-command更改另一个文件中的full_sample_name和sample_name.我知道如果您只读取一个文件,这是如何工作的,但是我无法使它适用于两个文件.

I have two .txt files that I want to read line per line simultaneously in .sh script. Both .txt files have the same number of lines. Inside the loop I want to use the sed-command to change the full_sample_name and sample_name in another file. I know how this works if you just read one file, but I cannot get it work for two files.

#! /bin/bash

FULL_SAMPLE="file1.txt"
SAMPLE="file2.txt"

while read ... && ...
do
    sed -e "s/\<full_sample_name\>/$FULL_SAMPLE/g" -e "s/\<sample_name\>/$SAMPLE/g" pipeline.sh > $SAMPLE.sh

done < ...?

推荐答案

#!/bin/bash

full_sample_file="file1.txt"
sample_file="file2.txt"

while read -r -u 3 full_sample_name && read -r -u 4 sample_name; do
    sed -e "s/\<full_sample_name\>/$full_sample_name/g" \
        -e "s/\<sample_name\>/$sample_name/g" \
        pipeline.sh >"$sample_name.sh"
done 3<"$full_sample_file" 4<"$sample_file" # automatically closed on loop exit

在这种情况下,我将文件描述符3分配给file1.txt,将文件描述符4分配给file2.txt.

In this case, I'm assigning file descriptor 3 to file1.txt and file descriptor 4 to file2.txt.

通过bash 4.1或更高版本,您不再需要手动处理文件描述符:

By the way, with bash 4.1 or newer, you no longer need to handle file descriptors manually:

# opening explicitly, since even if opened on the loop, these need
# to be explicitly closed.
exec {full_sample_fd}<file1.txt
exec {sample_fd}<file2.txt

while read -r -u "$full_sample_fd" full_sample_name \
   && read -r -u "$sample_fd" sample_name; do
  : do stuff here with "$full_sample_name" and "$sample_name"
done

# close the files explicitly
exec {full_sample_fd}>&- {sample_fd}>&-


另外一点:如果不保证您的 sample_name full_sample_name 值不能保证在自己的情况下求值,您可以提高效率(也可以更正确).如果您的输入文件不包含文字NUL(作为Shell脚本,则不应该),并且如果箭头括号意在使用文字而不是字边界的正则表达式字符,则解释为正则表达式,方法是不使用 sed ,但是只需读取要转换为shell变量的输入,然后在其中进行替换即可!


One more note: You could make this a bit more efficient (and also more correct, if your sample_name and full_sample_name values aren't guaranteed to evaluate to themselves when interpreted as regular expressions, if your input file contains no literal NULs [which, as a shell script, it shouldn't], and if the arrow brackets are intended to be literal rather than word-boundary regex characters) by not using sed at all, but just reading the input to be converted into a shell variable, and doing the replacements there!

exec {full_sample_fd}<file1.txt
exec {sample_fd}<file2.txt
IFS= read -r -d '' input_file <pipeline.sh

while read -r -u "$full_sample_fd" full_sample_name \
   && read -r -u "$sample_fd" sample_name; do
  output=${input_file//'<full_sample_name>'/${full_sample_name}}
  output=${output//'<sample_name>'/${sample_name}}
  printf '%s' "$output" >"${sample_name}.sh"
done

# close the files explicitly
exec {full_sample_fd}>&- {sample_fd}>&-

这篇关于读取bash中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆