2011-05-31 80 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

相關問題