Windows脚本:文本字符串的文本操作

时间:2012-09-05 20:41:36

标签: windows text sed cmd gawk

再次需要大帮助

File1中:

\\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

1 个答案:

答案 0 :(得分: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