正则表达式 – 如何从两个文件读取内容并合并到bash shell中的第
发布时间:2020-12-13 22:56:09 所属栏目:百科 来源:网络整理
导读:在bash中如何读取/处理2个文件同步? 我有2个文本文件,其中有相同数量的行/项目. 一个文件是 abc 另一个文件是 123 如何循环通过这些文件同步,以便a与1,b- 2,c- 3相关联? 我以为我可以读取文件作为一个数组,然后用索引处理它,但似乎我的语法/逻辑不正确. 所
在bash中如何读取/处理2个文件同步?
我有2个文本文件,其中有相同数量的行/项目. a b c 另一个文件是 1 2 3 如何循环通过这些文件同步,以便a与1,b-> 2,c-> 3相关联? 我以为我可以读取文件作为一个数组,然后用索引处理它,但似乎我的语法/逻辑不正确. 所以做f1 = $(cat file1)使f1 = a b c.我以为做f1 =($(cat file1))会使它成为一个数组,但它使f1 = a,因此没有数组来处理. 如果有人想知道我搞砸了什么代码: hostnames=($(cat $host_file)) # trying to read in as an array,which apparently is incorrect roles=($(cat $role_file)) for i in {0..3} do echo ${hostnames[$i]} # wanted to iterate through each element in the file/array # but there is only one object instead of N objects echo ${roles[$i]} done
你的方式:
host_file=host1 role_file=role1 hostnames=( $(cat $host_file) ) roles=( $(cat $role_file) ) (( cnt = ${#hostnames[@]} -1 )) echo "cnt is $cnt" for (( i=0;i<=$cnt;i++)) do echo "${hostnames[$i]} -> ${roles[$i]}" done (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |