在bash中遍历值对我有10个文本文件,我想paste每个文件与它的对,这样我有5个总文件。我尝试了以下几点:for i in 4_1 5_1 6_1 7_1 8_1dofor j in 4_2 5_2 6_2 7_2 8_2dopaste ${i}.txt ${j}.txt > ${i}.${j}.txtdonedone但是,这段代码结合了所有可能的组合,而不仅仅是组合匹配的对。所以我想要文件4_1.txt成对4_2.txt, 5_1.txt带着5_2.txt等
3 回答
莫回无
TA贡献1865条经验 获得超7个赞
for file in 4 5 6 7 8do paste "${file}_1" "${file}_2"done
paste 4_1 4_2paste 5_1 5_2...
呼如林
TA贡献1798条经验 获得超3个赞
declare -A pairs=( [4_1]=4_2 [5_1]=5_2 [6_1]=6_2 [7_1]=7_2 [8_1]=8_2 )for i in "${!pairs[@]}"; do j=${pairs[$i]} paste "$i.txt" "$j.txt" >"${i}.${j}.txt"done
is=( 4_1 5_1 6_1 7_1 8_1 )js=( 4_2 5_2 6_2 7_2 8_2 )for idx in "${!is[@]}"; do i=${is[$idx]} j=${js[$idx]} paste "$i.txt" "$j.txt" >"$i.$j.txt"done
慕婉清6462132
TA贡献1804条经验 获得超2个赞
animals=(dog cat mouse)declare -A size=( [dog]=big [cat]=medium [mouse]=small)declare -A sound=( [dog]=barks [cat]=purrs [mouse]=cheeps)for animal in "${animals[@]}"; do echo "$animal is ${size[$animal]} and it ${sound[$animal]}"done
添加回答
举报
0/150
提交
取消