Linux bash脚本同时处理2个文件夹

这是我的bash可执行命令:

while read line do ./ngram -order 1 -lm path1/$line -ppl path2/$line -debug 4 > path3/$line done < input_list_of_files 

所以,我有两个文件夹在path1中,另一个在path2中。 path1和path2具有相同的文件名,但具有不同的扩展名。 例如,Path1有许多扩展名为“.txt”(file1.txt)的文件,path2有许多扩展名为“.title”(file1.title)的文件。

也就是说,path1具有文件夹folder1,其具有文件file1.txt,file2.txt,file3.txt等。类似地,path2具有文件夹folder2,其具有诸如file1.title,file2.title,file3.title和等等..

list_of_files有数据:

 file1.txt file2.txt file3.txt 

等等…

我想在“-lm”选项后inputfile1.txt,在“-ppl”选项后面inputfile1.title。 当我一次为一个单独的文件操作它时,这工作正常。

也就是说,当在“-lm”之后inputfile1.txt时,同时在“-ppl”之后应该有file1.title。

我想通过同时input相同的文件名,但不同的扩展名同时对文件夹中的所有文件进行批量计算。 我该怎么做? 请帮忙!

我用过的例子:

 ./ngram -order 1 -lm Path1/Army_recruitment.txt -ppl Path2/Army_recruitment.title -debug 4 > Path3/Army_recruitment.txt 

输出文件如下所示:

  military troop deployment number need p( military | <s> ) = [1gram] 0.00426373 [ -2.37021 ] p( troop | military ...) = [1gram] 0.00476793 [ -2.32167 ] p( deployment | troop ...) = [1gram] 0.00045413 [ -3.34282 ] p( number | deployment ...) = [1gram] 0.0015224 [ -2.81747 ] p( need | number ...) = [1gram] 0.000778574 [ -3.1087 ] p( </s> | need ...) = [OOV] 0 [ -inf ] 1 sentences, 5 words, 0 OOVs 1 zeroprobs, logprob= -13.9609 ppl= 619.689 ppl1= 3091.84 5 words, rank1= 0 rank5= 0 rank10= 0 6 words+sents, rank1wSent= 0 rank5wSent= 0 rank10wSent= 0 qloss= 0.998037 absloss= 0.998036 file Army_recruitment_title.txt: 1 sentences, 5 words, 0 OOVs 1 zeroprobs, logprob= -13.9609 ppl= 619.689 ppl1= 3091.84 5 words, rank1= 0 rank5= 0 rank10= 0 6 words+sents, rank1wSent= 0 rank5wSent= 0 rank10wSent= 0 qloss= 0.998037 absloss= 0.998036 

这个输出是根据可执行文件./ngram生成的。 这是一个包。

 # As suggested by @CharlesDuffy: use read -r to ensure that text is taken literally while read -r line ; do name="${line%.txt}" # Strip off .txt extension ./ngram -order 1 -lm "path1/$name.txt" -ppl "path2/$name.title" -debug 4 > "path3/$name" done < input_list_of_files 

除了目录名称之外,可以使用命令basename路径后缀。 所以:

 while read line do path2file=$(basename $line .txt).title ./ngram -order 1 -lm path1/$line -ppl path2/$path2file -debug 4 > path3/$line done < input_list_of_files 

(假定你仍然希望在输出文件的末尾输入.txt