如何遍历两组数据?

how to iterate over two sets of data?

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

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

/foo/file
/bar/file

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

/bar/file

我想到的一种方法是 mount | grep bind | awk '{print " " }',然后将其迭代到 sed 输出的每一行,然后是 sort -u.

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

将 'mount | grep bind' 命令放入 AWK 的 BEGIN 块中并存储数据。 类似于:

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] = ;
                  mountPt[count] = 
               }
          }
          # 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(, mountPt[i]);
                 if (idx == 1) {
                   replaceLine = 1;
                   break;
                 }
               }
               if (replaceLine == 1) {
                 sub(mountPt[i], mount[i], );
               }
               if (printed[] != 1) {
                 print ;              
               } 
               printed[] = 1;    
           } ' 

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

find YourPath -print > YourFiles.txt
mount > Bind.txt

awk 'FNR == NR && [=10=] ~ /bind/ {
        Bind[ ] = 
        if( ( ThisLevel = split( , Unused, "/") - 1 ) > Level) Level = ThisLevel
        }
     FNR != NR && [=10=] !~ /^ *$/ {
        RealName = [=10=]
        for( ThisLevel = Level; ThisLevel > 0; ThisLevel--){
           match( [=10=], "(/[^/]*){" ThisLevel "}" )
           UnBind = Bind[ substr( [=10=], 1, RLENGTH) ]
           if( UnBind !~ /^$/) {
              RealName = UnBind substr( [=10=], RLENGTH + 1)
              ThisLevel = 0
              }
           }
        if( ! File[ RealName]++) print RealName
        }

   ' Bind.txt YourFiles.txt
  • 根据首先加载的绑定数组的精确 path/bind 比较进行搜索
  • Bind.txt 和 YourFiles.txt 可能是直接重定向为“1”指令并且没有临时文件
  • 如果绑定中的路径使用 space 字符(假设不在此处),则必须进行调整(awk 的第一部分)
  • 文件路径在读取时实时更改,与现有绑定关系相比
  • 打印文件如果还不知道