将 file1 的每一行与 file2 的每一行进行比较

将 file1 的每一行与 file2 的每一行进行比较

我有2个文件。

c1日志

D1W /u02/app/oracle/db/ggs/current/dirdat/fe
D2W /u02/app/oracle/db/ggs/current/dirdat/ff

c2日志

E1W /u02/app/oracle/db/ggs/current/dirdat/fe
E2W /u02/app/oracle/db/ggs/current/dirdat/fe
E3W /u02/app/oracle/db/ggs/current/dirdat/fe
F1W /u02/app/oracle/db/ggs/current/dirdat/ff
F2W /u02/app/oracle/db/ggs/current/dirdat/ff
F3W /u02/app/oracle/db/ggs/current/dirdat/ff
F4W /u02/app/oracle/db/ggs/current/dirdat/ff

我必须将 c1.log 中每一行的第二列与 c2.log 中每一行的第二列进行比较。如果匹配,那么它应该在一行中打印输出

D1W /u02/app/oracle/db/ggs/current/dirdat/fe    E1W /u02/app/oracle/db/ggs/current/dirdat/fe

此外,输出应记录在新文件中。假设 c3.log 请帮忙。

谢谢摩西

答案1

我为你写了一个简单的 python 脚本,它可以做到这一点:

address1 = input("Enter first file address:\n")
address2 = input("Enter second file address:\n")

data1 = []
data2 = []
with open(address1) as file:
    data1 = file.read().split('\n')

with open(address2) as file:
    data2 = file.read().split('\n')

if data1[-1] == "": data1.pop()
if data2[-1] == "": data2.pop()

for i in data1:
    for j in data2:
        if i.split(' ')[1].strip() == j.split(' ')[1].strip():
            print(i + ' ' + j)

答案2

join可以为您完成大部分工作。它连接两个文件中的匹配行,在第二个字段上匹配,如下所示:

$ join -j 2 c1.log c2.log
/u02/app/oracle/db/ggs/current/dirdat/fe D1W E1W
/u02/app/oracle/db/ggs/current/dirdat/fe D1W E2W
/u02/app/oracle/db/ggs/current/dirdat/fe D1W E3W
/u02/app/oracle/db/ggs/current/dirdat/ff D2W F1W
/u02/app/oracle/db/ggs/current/dirdat/ff D2W F2W
/u02/app/oracle/db/ggs/current/dirdat/ff D2W F3W
/u02/app/oracle/db/ggs/current/dirdat/ff D2W F4W

这将为您提供匹配项以及 c1 和 c2 的第一列。 Join 需要对输入文件进行排序。如果不是,您可以即时对它们进行排序:
join -j2 <(sort c1.log) <(sort c2.log)。这是贝壳魔法。<( some command )看起来像一个文件join或任何其他需要文件的命令。

让我们进一步处理这些:

$ join -j 2 c1.log c2.log | while read match c1 c2; do printf '%s %s\t%s %s\n' $c1 $match $c2 $match; done
D1W /u02/app/oracle/db/ggs/current/dirdat/fe    E1W /u02/app/oracle/db/ggs/current/dirdat/fe
D1W /u02/app/oracle/db/ggs/current/dirdat/fe    E2W /u02/app/oracle/db/ggs/current/dirdat/fe
D1W /u02/app/oracle/db/ggs/current/dirdat/fe    E3W /u02/app/oracle/db/ggs/current/dirdat/fe
D2W /u02/app/oracle/db/ggs/current/dirdat/ff    F1W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff    F2W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff    F3W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff    F4W /u02/app/oracle/db/ggs/current/dirdat/ff

完毕!

以下是精心包装的命令,因此您可以根据需要将它们粘贴到脚本中:

join -j 2 c1.log c2.log \
| while read match c1 c2; do 
  printf '%s %s\t%s %s\n' "$c1" "$match" "$c2" "$match"
done

如果您不需要两个部分之间的标签@bu5hman 有更好的答案

更新:
您也可以使用 join 来插入制表符: join -t' ' -j2 -o 1.1 1.2 2.1 2.2 <(sort c1.log) <(sort c2.log) 后面的空格-t'是文字制表符。按 Ctrl+V,然后按 TAB 键。另外,我正在对文件进行内联排序。

$ join -t'      ' -j2 -o 1.1 1.2 2.1 2.2 <(sort c1.log) <(sort c2.log)
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        E1W /u02/app/oracle/db/ggs/current/dirdat/fe    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        E2W /u02/app/oracle/db/ggs/current/dirdat/fe    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        E3W /u02/app/oracle/db/ggs/current/dirdat/fe    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        F1W /u02/app/oracle/db/ggs/current/dirdat/ff    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        F2W /u02/app/oracle/db/ggs/current/dirdat/ff    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        F3W /u02/app/oracle/db/ggs/current/dirdat/ff    
D1W /u02/app/oracle/db/ggs/current/dirdat/fe        F4W /u02/app/oracle/db/ggs/current/dirdat/ff    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        E1W /u02/app/oracle/db/ggs/current/dirdat/fe    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        E2W /u02/app/oracle/db/ggs/current/dirdat/fe    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        E3W /u02/app/oracle/db/ggs/current/dirdat/fe    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        F1W /u02/app/oracle/db/ggs/current/dirdat/ff    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        F2W /u02/app/oracle/db/ggs/current/dirdat/ff    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        F3W /u02/app/oracle/db/ggs/current/dirdat/ff    
D2W /u02/app/oracle/db/ggs/current/dirdat/ff        F4W /u02/app/oracle/db/ggs/current/dirdat/ff

答案3

或者,假设您的文件已排序-k2

join -j2 -o 1.1 1.2 2.1 2.2 c1.log c2.log

输出:

D1W /u02/app/oracle/db/ggs/current/dirdat/fe E1W /u02/app/oracle/db/ggs/current/dirdat/fe
D1W /u02/app/oracle/db/ggs/current/dirdat/fe E2W /u02/app/oracle/db/ggs/current/dirdat/fe
D1W /u02/app/oracle/db/ggs/current/dirdat/fe E3W /u02/app/oracle/db/ggs/current/dirdat/fe
D2W /u02/app/oracle/db/ggs/current/dirdat/ff F1W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff F2W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff F3W /u02/app/oracle/db/ggs/current/dirdat/ff
D2W /u02/app/oracle/db/ggs/current/dirdat/ff F4W /u02/app/oracle/db/ggs/current/dirdat/ff

答案4

假设 c1.log、c2.log 和以下脚本存在于同一目录中,这应该可以工作。

while read i
do
 c1_array=( $i )
 string1=${c1_array[1]}
 while read j
 do
   c2_array=( $j )
   string2=${c2_array[1]}
   if [ $string1 = $string2 ]
   then
    echo "$i    $j"
   fi
 done < c2.log
done < c1.log

相关内容