我正试图遍历一个大目录(500个文件(。我需要查看每个文件,并检查它们是否包含特定的字符串。字符串来自我的另一个文件,它看起来像这样:
user1
user2
user3
...
现在找出所有具有";用户";其中很容易使用:
grep -wFf my_list /path/to/dir/*
这给了我
/path/to/dir/file1:#user1 <-- the "users" are added as comments to the file
/path/to/dir/file2:#user1
/path/to/dir/file2:#user2
...
然而,在这一点上,我想使用CCD_;用户";作为论据,我不知道如何继续下去。
我能想到的唯一方法是读取数组中的所有用户(~1600(,然后使用for循环在每个文件中搜索每个用户,然后在找到匹配项时运行我的命令,但这看起来会非常耗费计算。有更好的方法吗?
将输出通过管道传输到while read
循环以执行其他命令。
grep -wFf my_list /path/to/dir/* | while IFS=: read -r filename user; do
# code that users $filename and $user
done