grep文件并匹配一行中的字符串

时间:2017-09-01 00:26:32

标签: unix grep

我的文件中有以下行。我想grep我的日志文件中的所有行,其中baseFileName不等于我的文件名。在下面的例子中,它应该打印第1行,因为TEST boo.docx不等于Test foo-boo.docx。

<textarea>

1 个答案:

答案 0 :(得分:1)

因为这个问题有一个unix-tag:

grep -P "baseFileName=([^,]+),.*,\1" test.txt

说明:

baseFileName=([^,]+)       # first pair of parentheses gives you baseFilename                   
,.*,                       # read ',' followed by anything, followed by ','
\1                         # backreference to baseFilename

测试:

$ cat test.txt
2017-06-19 21:54:11,773 mimeType=docx,baseFileName=TEST 
boo.docx,fileNamePrefix=7ff852cb-b1db-49d3-ba71-
e151dbc1f41e,doEncrypt=true,decryptedFileSize=125589,Test foo-boo.docx    
[source:MessageConsumer]

2017-06-19 21:54:11,774 mimeType=docx,baseFileName=TEST 
foo.docx,fileNamePrefix=7ff852cb-b1db-49d3-ba71-
e151dbc1f41e,doEncrypt=true,decryptedFileSize=125589,TEST foo.docx    
[source:MessageConsumer]
2017-06-19 21:54:11,774 
mimeType=docx,baseFileName=aaa.docx,fileNamePrefix=7ff852cb-b1db-49d3-
ba71-e151dbc1f41e,doEncrypt=true,decryptedFileSize=125589,aaa.docx    
[source:MessageConsumer]

$ grep -P "baseFileName=([^,]+),.*,\1" test.txt
2017-06-19 21:54:11,774 mimeType=docx,baseFileName=TEST 
foo.docx,fileNamePrefix=7ff852cb-b1db-49d3-ba71-
e151dbc1f41e,doEncrypt=true,decryptedFileSize=125589,TEST foo.docx    
[source:MessageConsumer]
2017-06-19 21:54:11,774 
mimeType=docx,baseFileName=aaa.docx,fileNamePrefix=7ff852cb-b1db-49d3-
ba71-e151dbc1f41e,doEncrypt=true,decryptedFileSize=125589,aaa.docx    
[source:MessageConsumer]