正则表达式 – 如何从两个文件读取内容并合并到bash shell中的第三个文件

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何从两个文件读取内容并合并到bash shell中的第三个文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在bash中如何读取/处理2个文件同步?

我有2个文本文件,其中有相同数量的行/项目.
一个文件

  1. a
  2. b
  3. c

另一个文件

  1. 1
  2. 2
  3. 3

如何循环通过这些文件同步,以便a与1,b-> 2,c-> 3相关联?

我以为我可以读取文件作为一个数组,然后用索引处理它,但似乎我的语法/逻辑不正确.

所以做f1 = $(cat file1)使f1 = a b c.我以为做f1 =($(cat file1))会使它成为一个数组,但它使f1 = a,因此没有数组来处理.

如果有人想知道我搞砸了什么代码

  1. hostnames=($(cat $host_file))
  2. # trying to read in as an array,which apparently is incorrect
  3. roles=($(cat $role_file))
  4.  
  5. for i in {0..3}
  6. do
  7. echo ${hostnames[$i]}
  8. # wanted to iterate through each element in the file/array
  9. # but there is only one object instead of N objects
  10. echo ${roles[$i]}
  11. done
你的方式:
  1. host_file=host1
  2. role_file=role1
  3.  
  4. hostnames=( $(cat $host_file) )
  5. roles=( $(cat $role_file) )
  6. (( cnt = ${#hostnames[@]} -1 ))
  7. echo "cnt is $cnt"
  8. for (( i=0;i<=$cnt;i++))
  9. do
  10. echo "${hostnames[$i]} -> ${roles[$i]}"
  11. done

猜你在找的正则表达式相关文章