使用Bash或Perl将一行文本替换为目录中的多个文件

时间:2018-08-29 21:44:59

标签: linux bash perl file

我的目录中包含大约9000个文件,所有文件都具有相同的结构,如下所示:

      seqfile = brown.nuc * sequence data file name 
      outfile = mlb * main result file 
     treefile = brown.trees * tree structure file name 
        noisy = 3   * 0,1,2,3: how much rubbish on the screen 
      verbose = 0   * 1: detailed output, 0: concise output 
      runmode = 0   * 0: user tree;  1: semi-
automatic;  2: automatic 
                    * 3: StepwiseAddition; (4,5):PerturbationNNI  

我想用包含在txt文件列表中的单词替换“ seqfile = brown.nuc”行,列表就是这样,并且

organism_1.fna 
organism_2.fna 
organism_3.fna 
organism_4.fna 
organism_5.fna

我要获取的输出将是

seqfile = organism_1.fna * sequence data file name 
  outfile = mlb * main result file 
 treefile = brown.trees * tree structure file name 
    noisy = 3   * 0,1,2,3: how much rubbish on the screen 
  verbose = 0   * 1: detailed output, 0: concise output 
  runmode = 0   * 0: user tree;  1: semi-

 seqfile = organism_2.fna * sequence data file name 
      outfile = mlb * main result file 
     treefile = brown.trees * tree structure file name 
        noisy = 3   * 0,1,2,3: how much rubbish on the screen 
      verbose = 0   * 1: detailed output, 0: concise output 
      runmode = 0   * 0: user tree;  1: semi-



seqfile = organism_2.fna * sequence data file name 
      outfile = mlb * main result file 
     treefile = brown.trees * tree structure file name 
        noisy = 3   * 0,1,2,3: how much rubbish on the screen 
      verbose = 0   * 1: detailed output, 0: concise output 
      runmode = 0   * 0: user tree;  1: semi-

我一直在尝试使用Sed和Awk,但无法获得输出。

1 个答案:

答案 0 :(得分:-1)

for file in *.ext; do 
    for replacement in $(<replacements.file) ; do 
        sed "s/brown\.nuc/$replacement/" < "$file"; 
    done > "$file.processed"; 
done

这会将更改后的数据放入$originalFilename.processed中,以后您可以根据需要在原始文件上进行复制。