2011-05-31 114 views
0

如何从一个文件中,包含在另一个文件中如何从一个文件中获取内容,在另一个文件包含

例获得行,我有

 
// first 
foo 
bar 
 
// second 
foo;1;3;p1 
bar;1;3;p2 
foobar;1;3;p2 

此文件大,第一个文件包含〜50万点的记录,和第二〜20-15百万

我需要得到这个结果

 
// attention there is no "p1" or "p2" for example 
foo;1;3 
bar;1;3 

回答

2

这看起来像是想要join命令,可能与排序。但有了数百万条记录,现在该认真思考一个真正的DBMS。

join -t\; -o 0,2.2,2.3 <(sort -t\; -k 1,1 first) <(sort -t\; -k 1,1 second) 

(这需要bashzsh<(command)语法,可移植,则需要分类到临时文件或保持排序的输入文件。)

1

grep -f:

-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.) 

cut -d \; -f1-3:

-d, --delimiter=DELIM 
      use DELIM instead of TAB for field delimiter 

-f, --fields=LIST 
      select only these fields; also print any line that contains no 
      delimiter character, unless the -s option is specified 

把它放在一起:grep -f pattern_file data_file | cut -d\; -f1-3

+0

这不会满足'//注意有例如不是“p1”或“p2”。 – geekosaur 2011-05-31 14:07:23

+0

谢谢@geekosaur。 – 2011-05-31 14:14:44

+0

'grep -f'不起作用,对我来说它只返回最后匹配的字符串 – azat 2011-05-31 18:35:29

相关问题