0

再次需要大帮助

文件1:

\\server2\share1,\\serverdfs\dfs\test2
\\server1\anothershare,\\serverdfs\dfs\test1

文件2:

\\server2\share1,/fs7/server1/share1
\\server1\anothershare,/fs1/serverx/share1
\\server4\newshare,/fs7/server1/share1

脚本:

@ECHO OFF 
DEL file3 2 > nul 
FOR /f "tokens=1*delims=," %%i IN (file1.txt) DO ( 
  FOR /f "tokens=1*delims=," %%a IN (file2.txt) DO ( 
   IF /i "%%i"=="%%a" >>file3 ECHO %%i,%%j,%%b>file3.txt
  ) 
) 

输出是这样的:

\\server2\share1,\\\serverdfs\dfs\test2,/fs7/server1/share1  
\\server1\anothershare,\\\serverdfs\dfs\test1,/fs1/serverx/share1  

该脚本将在两个文件(file1.txt 和 file2.txt)之间搜索匹配项(第一个字段 = 逗号前的任何内容)。

例如,问题\\server4\newshare仅存在于 file2.txt 中。

在这种情况下,这两行都具有相同的第二个字段:

\\server2\share1,/fs7/server1/share1
\\server4\newshare,/fs7/server1/share1

你能想出什么来显示类似这样的输出:

\\server2\share1,\\\serverdfs\dfs\test2,/fs7/server1/share1  
\\server1\anothershare,\\\serverdfs\dfs\test1,/fs1/serverx/share1  

only_at_file2.txt, \\server4\newshare,/fs7/server1/share1 same path as \\server2\share1

4

1 回答 1

1

像这样运行:

awk -f script.awk file1.txt file2.txt

内容script.awk

BEGIN {
   FS=OFS=","
}

FNR==NR {
   array[$1]=$2
   next
}

{
   if ($1 in array) { 
      print $1, "\\"array[$1], $2
   }

   else {
      if ($2 in values) {
         print "only_at_" FILENAME ", " $0 " same path as " values[$2]
      }
      else {
         print "only_at_" FILENAME ", " $0 " no path found"
      }
   }
   values[$2]=$1
}

结果:

\\server2\share1,\\\serverdfs\dfs\test2,/fs7/server1/share1
\\server1\anothershare,\\\serverdfs\dfs\test1,/fs1/serverx/share1
only_at_file2.txt, \\server4\newshare,/fs7/server1/share1 same path as \\server2\share1

如果有重复的路径,这将显示找到的最后一个路径file2.txt(即values[$2]=$1覆盖以前的路径)。例如,如果现在在/fs7/server1/share1中找到另一个路径为file2.txt的服务器,则输出服务器将是\\server4\newshare

于 2012-09-05T23:42:35.253 回答