2012-09-05 42 views
0

需要很大的幫助再次Windows腳本:文本字符串

文件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,\\\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

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

你能爲了顯示類似這樣的輸出想到什麼

運行,如:

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將覆蓋前面的路徑)。例如,如果現在在file2.txt中找到另一臺路徑爲/fs7/server1/share1的服務器,則輸出服務器將爲\\server4\newshare

+0

非常感謝您解決此問題。 – Eduardo