遍历在bash值对 [英] Looping over pairs of values in bash

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

问题描述

我有10个文本文件,我想粘贴每个文件与它配对,这样我有5个总文件。

I have 10 text files and I want to paste each file with its pair, such that I have 5 total files.

我试过如下:

for i in 4_1 5_1 6_1 7_1 8_1
do
for j in 4_2 5_2 6_2 7_2 8_2
do
paste ${i}.txt ${j}.txt > ${i}.${j}.txt
done
done

不过,这code每一个可能的组合,结合了刚刚结合匹配对吧。

However, this code combines every possible combination instead of just combining the matching pairs.

所以我想文件 4_1.txt 4_2.txt 5_1.txt 与 5_2.txt

推荐答案

如果你想使用一个变量,并用它执行和行动,你只需要使用一个循环:

If you want to use one variable and perform and action with it, you just need to use one loop:

for file in 4 5 6 7 8
do
   paste "${file}_1" "${file}_2"
done

这将做

paste 4_1 4_2
paste 5_1 5_2
...

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

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