2014-04-16 23 views
0

我有一个文件(maillog中)是这样的:壳:从FILE1获取一行内容FILE2

Feb 22 23:53:39 info postfix[102]: connect from APVLDPDF01[... 
    Feb 22 23:53:39 info postfix[101]: BA1D7805A1: client=APVLDPDF01[... 
    Feb 22 23:53:39 info postfix[103]: BA1D7805A1: message-id 
    Feb 22 23:53:39 info opendkim[139]: BA1D7805A1: DKIM-Signature field added 
    Feb 22 23:53:39 info postfix[763]: ED6F3805B9: to=<[email protected]>, relay... 
    Feb 22 23:53:39 info postfix[348]: ED6F3805B9: removed 
    Feb 22 23:53:39 info postfix[348]: BA1D7805A1: from=<[email protected]>,... 
    Feb 22 23:53:39 info postfix[102]: disconnect from APVLDPDF01... 
    Feb 22 23:53:39 info postfix[842]: 59AE0805B4: to=<[email protected]>,status=sent 
    Feb 22 23:53:39 info postfix[348]: 59AE0805B4: removed 
    Feb 22 23:53:41 info postfix[918]: BA1D7805A1: to=<[email protected]>, status=sent 
    Feb 22 23:53:41 info postfix[348]: BA1D7805A1: removed 

和第二文件(mailids)是这样的:

6DBDD8039F: 
    3B15BC803B: 
    BA1D7805A1: 
    2BD19803B4: 

我想得到一个包含如下内容的输出文件:

Feb 22 23:53:41 info postfix[918]: BA1D7805A1: to=<[email protected]>, status=sent 

只是ID在第二个文件中存在的行,在本例中只是ID = BA1D7805A1:在文件一中。但是还有另一个条件,这条线必须是“ID = <” 这意味着只有包含“to = <”的行和文件2中的ID才可以输出。

我发现不同的解决方案,但我有一个关于性能的巨大问题。 maillog文件大小为2GB,大约10万行。 mailid文件大约有32000行。

这个过程需要太多时间,我从来没有见过。 我试着用awk和grep命令,但我没有找到最好的方法。

+0

具体什么你尝试过这么远吗? – Mark

回答

2
grep -F -f mailids maillog | grep 'to=<' 

grep手册页:

-F, --fixed-strings 
      Interpret PATTERN as a list of fixed strings, separated by 
      newlines, any of which is to be matched. (-F is specified by 
      POSIX.) 

    -f FILE, --file=FILE 
      Obtain patterns from FILE, one per line. The empty file 
      contains zero patterns, and therefore matches nothing. (-f is 
      specified by POSIX.) 
+0

我想知道第二个grep可能会更快,也就是寻找'to = <'的那个,因为这样你就可以避免需要比较32,000个mailids的行,如果它不包含'to = <”。我可能是错的,这只是一个想法,我没有做任何基准测试。 –

+0

@MarkSetchell我也在想。 'grep'to = <'maillog | grep -F -f mailids' –

+0

此外,可能值得对maillog中的行进行计数,并使用“split”将其分成4或8个相等部分,并在背景中并行运行4或8个建议命令副本最后用“等待”来获得OP的其他3个或7个内核做一些有用的事情! –

1

最好添加-w选项

-w, --word-regexp 
      Select only those lines containing matches that form whole 
      words. The test is that the matching substring must either be 
      at the beginning of the line, or preceded by a non-word 
      constituent character. Similarly, it must be either at the end 
      of the line or followed by a non-word constituent character. 
      Word-constituent characters are letters, digits, and the 
      underscore. 

下面是常见的命令我使用。

grep -Fwf mailids maillog |grep 'to=<' 

,如果ID被固定在第6列,试试这个班轮awk命令

awk 'NR==FNR{a[$1];next} /to=</&&$6 in a ' mailids maillog 
相关问题