2017-09-01 87 views
-1

我在我的文件中有以下行。我想grep我的日誌文件中的所有行,其中baseFileName不等於我的文件名。在下面的情況下它應該打印第1行,因爲TEST boo.docx不等於Test foo-boo.docx。grep文件和匹配字符串在一行

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] 
+0

你說的「我的文件名是什麼意思「? – batMan

回答

1

由於這是問題有一個UNIX標籤:

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] 
+0

Thanks.I have a space'2017-06-19 21:54:11,773 mimeType = docx,baseFileName = TEST boo.docx,fileNamePrefix = 7ff852cb-b1db-49d3-ba71-e151dbc1f41e,doEncrypt = true,decryptedFileSize = 125589, 測試foo-boo.docx [來源:MessageConsumer]' – javascriptlearner

+0

在你的例子中,文件名是不同的。一個包含全部大寫字母,另一個僅以首都開頭。 –

+0

這是正確的,你在貓提到的例子是完美的,但只有我在我的日誌中有一個空間。 'mimeType = docx,baseFileName = aaa.docx,fileNamePrefix = 7ff852cb-b1db-49d3- ba71-e151dbc1f41e,doEncrypt = true,decryptedFileSize = 125589,aaa.docx [source:MessageConsumer]'。請不要在aaa.docx – javascriptlearner