gpt4 book ai didi

linux - 如何遍历两组数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:22 25 4
gpt4 key购买 nike

我正在尝试创建自己的程序来执行递归列表:每行对应于单个文件的完整路径。我现在正在处理的棘手部分是:我不想绑定(bind)挂载来欺骗我的程序两次列出文件。

所以我已经有一个程序可以产生正确的输出,除了如果 /foo 绑定(bind)安装到 /bar 那么我的程序会错误地列出

/foo/file
/bar/file

我需要程序列出下面的内容(编辑:即使它被要求列出 /foo 的内容)

/bar/file

我想到的一种方法是mount | grep 绑定(bind) | awk '{print $1 ""$3}' 然后遍历 sed 输出的每一行,然后 sort -u

我的问题是如何遍历原始输出(一堆行) mount 的输出(另一行)? (或者有更好的方法)这需要是 POSIX(编辑:并使用 /bin/sh)

最佳答案

放置'mount | grep bind' 命令到 AWK 的 BEGIN block 中并存储数据。像这样的东西:

PROG | awk 'BEGIN{ 
# Define the data you want to store
# Assign to global arrays
command = "mount | grep bind";
while ((command | getline) > 0) {
count++;
mount[count] = $1;
mountPt[count] = $3
}
}
# Assuming input is line-by-line and that mountPt is the value
# that is undesired
{
replaceLine=0
for (i=1; i<=count; i++) {
idx = index($1, mountPt[i]);
if (idx == 1) {
replaceLine = 1;
break;
}
}
if (replaceLine == 1) {
sub(mountPt[i], mount[i], $1);
}
if (printed[$1] != 1) {
print $1;
}
printed[$1] = 1;
} '

我假设您当前的程序 PROG 输出到标准输出。

关于linux - 如何遍历两组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com