如何从两个不同的文件中找到匹配的模式

时间:2014-05-27 21:40:22

标签: regex scripting

我有这两个文件,并希望找到匹配的模式 对于基于第一个文件中的条目的第二个文件,然后打印出来 匹配的结果。

这是第一个文件,

switch1  Ethernet2   4     4     5  
switch1  Ethernet4   4     4     5  
switch1  Ethernet7   4     4     5  
switch1  Ethernet9   4     4     5  
switch1  Ethernet10  4     4     5  
switch1  Ethernet13  1     4     5  
switch1  Ethernet14  1     4     5  
switch1  Ethernet15  1     4     5  
switch2  Ethernet5   4     4     5  
switch2  Ethernet6   1     4     5  
switch2  Ethernet7   4     5  
switch2  Ethernet8   1     4     5  
switch1  Ethernet1   2002  2697  2523  
switch1  Ethernet17  2002  2576  2515  
switch1  Ethernet11  2002  2621  2617  
switch2  Ethernet1   2001  2512  2577  

这是第二个文件,

switch1  Ethernet1   1   4  5  40  2002  2523  2590  2621  2656  2661  2665  2684  2697  2999  
switch1  Ethernet2   1   4  5  40  2002  2504  2505  2508  2514  2516  2517  2531  2533  2535  2544  2545  2547  2549  2555  2566  2571  2575  2589  2590  2597  2604  2611  2626  2629  2649  2666  2672  2684  2691  2695  
switch1  Ethernet3   40  
switch1  Ethernet4   1   4  5  40  2002  2504  2516  2517  2535  2547  2549  2571  2579  2590  2597  2604  2605  2620  2624  2629  2649  2684  2695  
switch1  Ethernet5   1   4  5  40  55    56    2000  2002  2010  2037  2128  2401  2409  2504  2526  2531  2540  2541  2548  2562  2575  2578  2579  2588  2590  2597  2604  2606  2608  2612  2615  2616  2621  2638  2640  2645  2650  2666  2667  2669  2670  2674  2678  2684  2690  2696  
switch1  Ethernet6   40  
switch1  Ethernet7   1   4  5  40  2037  2128  2174  2401  2409  2463  2526  2535  2540  2541  2544  2562  2578  2579  2590  2616  2621  2625  2631  2645  2659  2667  2670  2674  2678  2682  2684  2690  2696  
switch1  Ethernet8   1   4  5  40  2037  2128  2396  2401  2409  2420  2531  2619  2640  2653  2658  2669  2677  2683  2684  
switch1  Ethernet9   1   4  5  40  2128  2169  2396  2401  2409  2420  2504  2515  2531  2553  2578  2597  2619  2621  2640  2658  2669  2677  2683  2684  2694  
switch1  Ethernet10  1   4  5  40  2079  2128  2169  2378  2396  2453  2509  2578  2591  2597  2621  2634  2641  2657  
switch1  Ethernet11  1   4  5  40  2002  2128  2169  2396  2453  2509  2512  2520  2526  2549  2552  2564  2571  2575  2589  2591  2597  2611  2617  2621  2634  2641  2657  2671  2676  2686  2694  
switch1  Ethernet12  1   4  5  40  2079  2378  2396  2453  2515  2531  2553  2597  2619  2621  2640  2657  2669  2677  2684  2694  
switch1  Ethernet13  1   4  5  40  2174  2396  2453  2463  2508  2524  2531  2536  2546  2567  2597  2629  2640  2657  2669  2674  2684  
switch1  Ethernet14  1   4  5  40  2524  2536  2544  2567  2575  2582  2628  2640  2659  2674  2681  2689  
switch1  Ethernet15  1   4  5  40  2515  2553  2575  2582  2621  2628  2640  2681  2689  2694  
switch1  Ethernet16  1   4  5  40  2513  2539  2544  2553  2561  2573  2575  2582  2619  2640  2670  2681  
switch1  Ethernet17  1   4  5  40  2002  2508  2513  2515  2531  2538  2539  2544  2547  2553  2561  2570  2573  2575  2576  2582  2586  2601  2608  2619  2621  2640  2658  2670  2681  

应显示结果,

switch1  Ethernet13  1     4     5  
switch1  Ethernet14  1     4     5  
switch1  Ethernet15  1     4     5  
switch1  Ethernet8   1     4     5  
switch1  Ethernet16  1     4     5  
switch1  Ethernet1   2002  2697  2523  
switch1  Ethernet17  2002  2576  2515  

对我来说,具有挑战性的部分是我不知道 如何比较第一个文件的一行到第二个文件的所有行的模式。在这种情况下,当第二个文件没有时,它有5个匹配的模式 一致的领域。某些行比其他行短,匹配字段可能与其他行不同。 任何想法或建议将不胜感激。

非常感谢。

1 个答案:

答案 0 :(得分:1)

这样的事情应该有用,但可能有更好的方法

#!/bin/bash

patternfile='file1'
exec 4<$patternfile
otherfile='file2'

while read -u4 pattern; do
    grep ".*`echo ""$pattern"" | sed 's/\s\+/ \\\+.* /g'`.*" $otherfile
done

P.S。这不会输出您想要的所有行: Ethernet8是一个switch2,另一个是switch1。修复正则表达式会非常难看,但你可以使用awk只获取你想要的记录。 以太网16甚至不在第一个文件中,我认为该行是错误的。 以太网1和以太网17的记录无序,我的解决方案无法处理。

相关问题