比较两个csv文件的第1列,找到最接近的> =&& <,然后打印对

时间:2016-07-26 21:20:34

标签: linux

以下是一些示例(大文件的片段)。第一个文件示例的file1大于file2,有些时间戳匹配,而有些则不合适。所需的输出将包含它们的标题,但如果太困难,那么我可以将它们放入以后。第二个文件示例的file1小于file2,因此我需要附加一个与file2行最匹配的file1行(使用> =和< logic)。正如您所看到的,以秒为单位的时间因文件和示例而异,因此有些内容会完全匹配,而有些内容会很接近。

我可以很容易地找到所有确切的时间匹配,但不是>和<比较。

这段代码很有用,但是遗漏了很多行。 IV&#39,E
尝试重新安排此代码以提供以下" Desired"输出,但没有
成功。

awk -F, '
BEGIN           {CNT+=2
            }
NR == FNR       {a[NR] = $0
             b[NR] = $1
             next
            }
$1 >= b[CNT]    {CNT++
            }
$1 <  b[CNT]    {print a[CNT-1]
             print $0, RS
            }
' file1 file2

文件1:

TIMEFORMATTED,G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC_NO(R    
2016/05/25 16:25:19,0,0,0,NO_DEF,-2147483647
2016/05/25 16:25:20,0,0,0,NO_DEF,-2147483648
2016/05/25 16:25:21,0,0,0,NO_DEF,-2147483649
2016/05/25 16:25:22,0,0,0,NO_DEF,-2147483650
2016/05/25 16:25:23,0,0,0,NO_DEF,-2147483651
2016/05/25 16:25:24,0,0,0,NO_DEF,-2147483652
2016/05/25 16:25:25,0,0,0,NO_DEF,-2147483653
2016/05/25 16:25:26,0,0,0,NO_DEF,-2147483654
2016/05/25 16:25:27,0,0,0,NO_DEF,-2147483655

文件2:

TIMEFORMATTED,HDR_SYNC,HDR_SEC,HDR_MSEC,G_CCSDS_VERSION,G_CCSDS_VERSION(RAW)
2016/05/25 16:25:22,464374526,1464193527,206,0,0
2016/05/25 16:25:26,464374526,1464193532,206,0,0
2016/05/25 16:25:31,464374526,1464193537,207,0,0

期望的输出:

TIMEFORMATTED,G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC
2016/05/25 16:25:22,0,0,0,NO_DEF,-2147483650
TIMEFORMATTED,HDR_SYNC,HDR_SEC,HDR_MSEC,G_CCSDS_VERSION,G_CCSDS_VERSION(RAw
2016/05/25 16:25:22,464374526,1464193527,206,0,0          
TIMEFORMATTED,G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC
2016/05/25 16:25:26,0,0,0,NO_DEF,-2147483654
TIMEFORMATTED,HDR_SYNC,HDR_SEC,HDR_MSEC,G_CCSDS_VERSION,G_CCSDS_VERSION(RAw
2016/05/25 16:25:26,464374526,1464193532,206,0,0    
TIMEFORMATTED,G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC
2016/05/25 16:25:27,0,0,0,NO_DEF,-   2147483655
TIMEFORMATTED,HDR_SYNC,HDR_SEC,HDR_MSEC,G_CCSDS_VERSION,G_CCSDS_VERSION(RAW
2016/05/25 16:25:31,464374526,1464193537,207,0,0

第二个例子:

文件1:

TIMEFORMATTED,G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC_NO(R
2014/04/07 16:00:30,0,0,0,NO_DEF,-2147483647
2014/04/07 16:00:35,0,0,0,NO_DEF,-2147483648
2014/04/07 16:00:40,0,0,0,NO_DEF,-2147483649
2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
2014/04/07 16:00:50,0,0,0,NO_DEF,-2147483651
2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653

文件2:

TIMEFORMATTED,CCSDS_VERSION,CCSDS_VERSION(RAW),CCSDS_TYPE,CCSDS_TYPE(RAW),CCSDS_2HDR_FLAG,CCSDS_2HDR_FLAG(RAW),ID
2014/04/07 16:00:43,0,0,0,0,1,1,544
2014/04/07 16:00:45,0,0,0,0,1,3,544
2014/04/07 16:00:47,0,0,0,0,1,1,544
2014/04/07 16:00:49,0,0,0,0,4,1,544
2014/04/07 16:00:51,0,0,0,0,1,1,544
2014/04/07 16:00:53,0,0,0,0,1,7,544
2014/04/07 16:00:55,0,0,0,0,8,1,544
2014/04/07 16:00:57,0,0,0,0,1,2,544
2014/04/07 16:00:59,0,0,0,0,3,1,544
2014/04/07 16:00:61,0,0,0,0,1,1,544
2014/04/07 16:00:63,0,0,0,0,1,9,544
2014/04/07 16:00:65,0,0,0,0,4,1,544
2014/04/07 16:00:67,0,0,0,0,1,1,544

输出:我更喜欢像第一个输出示例那样附加标题,但是 如果它更容易,我会接受它。

2014/04/07 16:00:40,0,0,0,NO_DEF,-2147483649
2014/04/07 16:00:43,0,0,0,0,1,1,544
2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
2014/04/07 16:00:45,0,0,0,0,1,3,544
2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
2014/04/07 16:00:47,0,0,0,0,1,1,544
2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
2014/04/07 16:00:49,0,0,0,0,4,1,544
2014/04/07 16:00:50,0,0,0,NO_DEF,-2147483651
2014/04/07 16:00:51,0,0,0,0,1,1,544
2014/04/07 16:00:50,0,0,0,NO_DEF,-2147483651
2014/04/07 16:00:53,0,0,0,0,1,7,544
2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
2014/04/07 16:00:55,0,0,0,0,8,1,544
2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
2014/04/07 16:00:57,0,0,0,0,1,2,544
2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
2014/04/07 16:00:59,0,0,0,0,3,1,544
2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653
2014/04/07 16:00:61,0,0,0,0,1,1,544

1 个答案:

答案 0 :(得分:0)

好的,这是。我将1,添加到file1,将2,添加到file2,以便我可以将它们再次分开。这是剧本的前两行。

然后在时间戳(现在是字段2),第3行上将两个文件合并在一起。假设文件已经排序,合并操作非常有效。我还在第一个字段上排序(次要),因为如果文件戳相同,我们希望file1中的行到达file2之前。

perl脚本执行:

  1. 删除第一个字段(将其保存到$1
  2. 如果$ 1 ==“1”,则从file1保存最近一行,但不打印。
  3. 当它看到类型“2”时,它会打印最近的类型“1”行($last)和类型“2”行。
  4. a.sh脚本:

    pa-dev00$ cat a.sh
    perl -pe 's/^/1,/' file1 > file1a
    perl -pe 's/^/2,/' file2 > file2a
    sort -m -t, -k2,2 -k1,1 file1a file2a | perl -ne 's/^(.),//; if ($1 == "1") {$last=$_; } else { print $last; print $_; }'
    

    这是输出

    pa-dev00$ ./a.sh
    2014/04/07 16:00:40,0,0,0,NO_DEF,-2147483649
    2014/04/07 16:00:43,0,0,0,0,1,1,544
    2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
    2014/04/07 16:00:45,0,0,0,0,1,3,544
    2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
    2014/04/07 16:00:47,0,0,0,0,1,1,544
    2014/04/07 16:00:45,0,0,0,NO_DEF,-2147483650
    2014/04/07 16:00:49,0,0,0,0,4,1,544
    2014/04/07 16:00:50,0,0,0,NO_DEF,-2147483651
    2014/04/07 16:00:51,0,0,0,0,1,1,544
    2014/04/07 16:00:50,0,0,0,NO_DEF,-2147483651
    2014/04/07 16:00:53,0,0,0,0,1,7,544
    2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
    2014/04/07 16:00:55,0,0,0,0,8,1,544
    2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
    2014/04/07 16:00:57,0,0,0,0,1,2,544
    2014/04/07 16:00:55,0,0,0,NO_DEF,-2147483652
    2014/04/07 16:00:59,0,0,0,0,3,1,544
    2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653
    2014/04/07 16:00:61,0,0,0,0,1,1,544
    2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653
    2014/04/07 16:00:63,0,0,0,0,1,9,544
    2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653
    2014/04/07 16:00:65,0,0,0,0,4,1,544
    2014/04/07 16:00:60,0,0,0,NO_DEF,-2147483653
    2014/04/07 16:00:67,0,0,0,0,1,1,544
    

    这应该非常有效。每个步骤都应该是线性的,因此它只会随着文件的大小而缩放。