Awk从文件中提取行,并将另一个文件作为索引

时间:2018-02-12 12:01:23

标签: file awk text-processing

我有两个文件:一个有100个值的索引,另一个包含很多信息我想从我的索引文件中提取信息。例如:

FILE1.TXT

-name1

-name2

-name3

File2.txt:

Read name: name1

sometext  
sometext  
Complete

Read name: name8 (not index)

sometext  
sometext  
Complete

Read name: name2

sometext  
sometext  
Complete

结果:

name1

sometext  
sometext  
Complete

name2

sometext  
sometext  
Complete

如何使用文本提取File2的名称,直到使用File1作为索引完成?我是awk的新手,我认为我需要使用正则表达式.. 我希望你理解,谢谢你的帮助:)。

1 个答案:

答案 0 :(得分:2)

awk 'NR==FNR && NF{ a[substr($1, 2)]; next } /^name/{ f=($1 in a) }f' file1.txt file2.txt 解决方案:

name1

sometext
sometext
Complete

name2

sometext
sometext
Complete

输出:

D <- read.table(header=TRUE, stringsAsFactors = FALSE, text=
"ID  Val1    Val2    Time
1   A         B     12:00
1   A         C     13:10
1   C         D     13:19
2   L         O     14:00
2   A         C     15:00
2   A         M     15:00
3   P         J     16:00")
Vc = c("A", "B", "C", "I", "T")   

 D %>% 
      group_by(ID) %>% 
      summarize(Match = sum(unique((Val1 %in% Vc),(Val2 %in% Vc)))) %>% 
      filter(Match > 0)