list - looping over different list files, retrieving always the sam Eline
问题描述
I have three files with a list of files I need to use later to apply a function. I have managed to create a loop going through the 3 different files, but no exactly the output I need. My input: rw_sorted_test.txt
1
2
3
fwd_sorted_test.txt
A
B
C
run_list.txt
1st
2nd
3rd
I am running it like this:
for f in `cat rw_sorted_test.txt`; do for l in `cat fwd_sorted_test.txt`; do for r in `cat run_list.txt` do echo ${f} ${l} ${r}; done; done; done;
What I am obtain now is something like:
1 A 1st
1 A 2nd
1 A 3rd
2 A 1st
2 A 2nd
2 A 3rd
3 A 1st
(...)
What I am looking for is something like:
1 A 1st
2 B 2nd
3 C 3rd
I am sure that it will be something simple, but I am really beginner and all the workarounds have not been working. Also, how can I then make it run after echo my desired output? Thank you
解决方案
快速尝试,如果这是您需要的:
exec 4< run_list.txt
exec 5< rw_sorted_test.txt
for a in $(cat fwd_sorted_test.txt); do
read run <&4
read sort <&5
echo "$sort $a $run"
done
...输出是:
1 1st A
2 2nd B
3 3rd C
文件也应该关闭:
exec 4<&-
exec 5<&-
重点是做一个循环,一次从 3 个不同的文件中读取一行。为输入而打开的文件 (exec ...< ...) 至少应包含与控制循环的主文件相同的行数。
可以在这里找到一些参考:文件描述符如何工作?
或对 bash 文件描述符进行一些研究。希望能帮助到你。
推荐阅读
- yarnpkg - 如何解决此错误:spawn yarn ENOENT?
- react-native - React Native Screen 在 iOS 中冻结滚动视图
- python - 如何获得相对于列表长度(即 100)的彩虹 RGV 值
- php - PHP Lighthouse Laravel - 通过认证用户获取资源
- c - 单读单写固定大小的环形缓冲区,没有锁和原子变量,对于任何 CPU 拱门总是安全的吗?
- python - 通过 __init__ 传递 configparser.ConfigParser() 对象?
- firebase - 如何从 firebase REST API 持久化 firebase 身份验证
- c++ - Qt Creator +CMake+ gmock build issue“gmock.h no such file or diretory”在Qt creator中构建项目时,如果通过命令行构建则没有问题
- sparql - SPARQL 和 blazegraph 的最短路径
- typescript - 缩小回调参数类型