循环遍历 bash 中的值对 [英] Looping over pairs of values in bash

查看:33
本文介绍了循环遍历 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

然而,这段代码结合了所有可能的组合,而不是仅仅结合匹配对.

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

所以我希望文件 4_1.txt4_2.txt 配对,5_1.txt5_2.txt

So I would like file 4_1.txt to be paired with 4_2.txt, 5_1.txt with 5_2.txt, etc.

推荐答案

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

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天全站免登陆