2013-07-17 44 views
0

的,我有以下文件: 2013-07-17_19-12-42.dcrec我如何可以隔離特定字符串的模式從這個正文

我如何搜索和文件中分離出以下字符串模式:

新名稱的客戶端0,keyID = 000000,IP = 000.000.000.000:somename

客戶端#可以是任何數字,keyid是任何數值(對於客戶端#或keyID沒有設置的長度),IP是任何正常的IPv4地址和somename可以是任何用戶名(用戶名可以包含特殊字符)作爲#,^,@,空格等)。它看起來像字符串'閉'與一個'^ Bvs'。這裏是2的字符串的一個例子(見截圖)

example 1

example 2

可以有在每個文件(一個或多個)任何數量的這些串。如果我可以在文件中搜索並列出這些字符串的所有實例,那將是非常好的。我現在對grep等不太好,否則我可以自己做這個。任何幫助將不勝感激,謝謝!

+1

這些截圖是一點點很難讀... –

回答

2
$> strings 2013-07-17_19-12-42.dcrec | grep -o -P "New name for client [0-9]+, keyID = [0-9]+, IP = [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} : [^\ ]+"       
New name for client 7, keyID = 562830, IP = 91.193.208.105 : Sobieski 
New name for client 8, keyID = 255344, IP = 63.153.210.124 : Cultist 
New name for client 11, keyID = 5061431, IP = 116.240.255.94 : Sammy 
New name for client 12, keyID = 5061453, IP = 196.20.195.114 : Dirk 
New name for client 13, keyID = 4278381, IP = 188.110.185.183 : CSTO 
New name for client 14, keyID = 369397, IP = 81.110.45.165 : General 
New name for client 16, keyID = 5061651, IP = 85.4.29.162 : Thatsuseless 
New name for client 17, keyID = 5061688, IP = 90.213.51.77 : NewPlayer 
New name for client 18, keyID = 4905930, IP = 174.109.181.108 : Solo 
New name for client 19, keyID = 5061695, IP = 85.4.236.70 : Quizzman 
New name for client 21, keyID = 2745089, IP = 95.128.68.231 : NewPlayer 
New name for client 22, keyID = 5061536, IP = 195.91.236.65 : POWERFUCKER 
New name for client 24, keyID = 5061698, IP = 86.121.66.142 : TheDoctor 
New name for client 26, keyID = 5061585, IP = 5.69.250.33 : Hydrogen 

說明:

  • 如果輸入文件是二進制格式它可以通過strings處理來檢索所有文字;
  • 客戶機#不應該是個[0-9]+
  • keyid的任何數值[0-9]+
  • 的IP是任何正常的IPv4地址[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
  • somename將任何用戶名(用戶名可以包含#,^,@,空格等特殊字符)[^\ ]+表示「無空格」

似乎第一次就夠了。是的,所有這些正則表達式都可以改進。

UPD:實際上,它看起來與.*正則表達式的最後一個字段(somename)更好

+0

對不起,花了這麼長的時間回覆,謝謝你的儘管幫助!這已經成功了 – lacrosse1991

1
grep --binary-files=text -o 'New name for client[^^B]*' 2013-07-17_19-12-42.dcrec 

[^^B]*部分是定期[^其次CTRL + v其次CTRL + b和常規],這意味着任何不是^B控制字符的字符。

輸出

New name for client 7, keyID = 562830, IP = 91.193.208.105 : Sobieski 
New name for client 8, keyID = 255344, IP = 63.153.210.124 : Cultist O Khorne 
New name for client 11, keyID = 5061431, IP = 116.240.255.94 : Sammy 
New name for client 12, keyID = 5061453, IP = 196.20.195.114 : Dirk Diggler 
New name for client 13, keyID = 4278381, IP = 188.110.185.183 : CSTO 
New name for client 14, keyID = 369397, IP = 81.110.45.165 : General Ivan 
New name for client 16, keyID = 5061651, IP = 85.4.29.162 : Thatsuseless 
New name for client 17, keyID = 5061688, IP = 90.213.51.77 : NewPlayer 
New name for client 17 (NewPlayer), keyID = 5061688, IP = 90.213.51.77 : MHT 
New name for client 18, keyID = 4905930, IP = 174.109.181.108 : Solo Wing Pixy 
New name for client 19, keyID = 5061695, IP = 85.4.236.70 : Quizzman 
New name for client 21, keyID = 2745089, IP = 95.128.68.231 : NewPlayer 
New name for client 18 (Solo Wing Pixy), keyID = 4905930, IP = 174.109.181.108 : Jane The Killer 
New name for client 22, keyID = 5061536, IP = 195.91.236.65 : POWERFUCKER 
New name for client 24, keyID = 5061698, IP = 86.121.66.142 : TheDoctor 
New name for client 26, keyID = 5061585, IP = 5.69.250.33 : Hydrogen 

如果您想篩選出像行:

New name for client 17 (NewPlayer), keyID = 5061688, IP = 90.213.51.77 : MHT 
New name for client 18 (Solo Wing Pixy), keyID = 4905930, IP = 174.109.181.108 : Jane The Killer 

使用上述的變型:

grep --binary-files=text -o 'New name for client [0-9]\+,[^^B]*' \ 
    2013-07-17_19-12-42.dcrec 
相關問題