简体   繁体   English

如何使用sed或awk在单独的文件中grep带有模式的行

[英]how to grep lines with patterns in a seperate file using sed or awk

I've a big data file ( data.txt ) and a pattern file ( patt.dat ) and the data is like below 我有一个大数据文件( data.txt )和一个模式文件( patt.dat ),数据如下

data.txt data.txt中

[bottle]:[some description 1]    
[pen]:[some description 2]    
[mobile]:[some description 3]       
[pen_pencil]:[some description 4]  
[mouse]:[some description 5]    

patt.dat patt.dat

pen    
mobile    

i give like this 我给这样

grep -F -f patt.dat data.txt    

then i get the below 然后我得到以下

[pen]:[some description 2]    
[mobile]:[some description 3]    
[pen_pencil]:[some description 4]    

but I want only want, 但我只想要

[pen]:[some description 2]    
[mobile]:[some description 3] 

Please help with any solution. 请提供任何解决方案的帮助。
I don't want to hard code anything because there'll be a lot of such patterns and hard-coding all won't look good. 我不想硬编码任何东西,因为会有很多这样的模式,而且硬编码看起来都不好。

If the same can be achieved in any other way possibly, please suggest that one too. 如果可以通过其他任何方式实现同​​样的效果,请也建议这样做。

Use -w for word match as well like: 使用-w进行单词匹配,例如:

grep -wf patt.dat data.txt 
Output:
[pen]:[some description 2]
[mobile]:[some description 3]
sed 's/.*/^\[&]/' patt.dat > /tmp/patt_grep.dat
egrep -F -f /tmp/patt_grep.dat data.txt

need to change a bit the patt.dat content to allow another efficient tool to work. 需要更改patt.dat内容,以允许另一个有效的工具运行。 I use egrep to allow the ^ but a (f)grep could be used changing the to format [word]:[ instead 我使用egrep允许^但可以使用(f)grep更改格式为[word]:[

Here is how to do it with awk 这是使用awk

awk -F"[][]" 'FNR==NR {a[$1];next} $2 in a' patt.dat data.txt
[pen]:[some description 2]
[mobile]:[some description 3]

If you like to test on more parameters, awk may be the simplest solution. 如果您想测试更多参数, awk可能是最简单的解决方案。
It stores the pattern in array a , then test if field 2 is found in array a , if found, print the line. 它将模式存储在数组a ,然后测试是否在数组a找到了字段2 ,如果找到,则打印该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM