2016-04-05 17 views
0

こんにちは私は、マカフィーのログファイルをトリムし、 "OKです"と私が見たいと思っていない他の報告されたインスタンスをすべて削除したいと考えています。 grepに-vオプションを利用したシェルスクリプトを使用する前に、今はLinuxとWindowsの両方で動作するpythonスクリプトを書くつもりです。数回の試行の後、私は正規表現をオンラインの正規表現ビルダーで使うことができましたが、スクリプトに実装するのは困難です。 Online REGEX BuilderPythonの逆マッチヘルプ

編集:「OK」、「壊れています」、「ブロック行です」、「ファイルを開くことができませんでした」行を削除したい場合は、私が興味を持ってちょうど問題のシェルでは、このようなのようなののソート:。私は、ファイルのサンプルを

import re 

f2 = open(outFilePath) 
contents = f2.read() 
print contents 
p = re.compile("^((?!(is OK)|(file could not be opened)| (is a broken)|(is a block)))*$", re.MULTILINE | re.DOTALL) 
m = p.findall(contents) 
print len(m) 
for iter in m: 
    print iter 
f2.close() 

grep -v "is OK" ${OUTDIR}/${OUTFILE} | grep -v "is a broken" | grep -v "file could not be opened" | grep -v "is a block" > ${OUTDIR}/${OUTFILE}.trimmed 2>&1 

私が読んで、ここでファイルを検索検索しようとしています:

eth0 
10.0.11.196 
00:0C:29:AF:6A:A7 
parameters passed to uvscan: --DRIVER /opt/McAfee/uvscan/datfiles/current -- ANALYZE --AFC=32 ATIME-PRESERVE --PLAD --RPTALL RPTOBJECTS SUMMARY --UNZIP -- RECURSIVE --SHOWCOMP --MIME --THREADS=4 /tmp 
temp XML output is: /tmp/HIQZRq7t2R 
McAfee VirusScan Command Line for Linux64 Version: 6.0.5.614 
Copyright (C) 2014 McAfee, Inc. 
(408) 988-3832 LICENSED COPY - April 03 2016 

AV Engine version: 5700.7163 for Linux64. 
Dat set version: 8124 created Apr 3 2016 
Scanning for 670707 viruses, trojans and variants. 


No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/ATIME-PRESERVE 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/RPTOBJECTS 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/SUMMARY 
/tmp/tmp.BQshVRSiBo ... is OK. 
/tmp/keyring-F6vVGf/socket ... file could not be opened. 
/tmp/keyring-F6vVGf/socket.ssh ... file could not be opened. 
/tmp/keyring-F6vVGf/socket.pkcs11 ... file could not be opened. 
/tmp/yum.log ... is OK. 
/tmp/tmp.oW75zGUh4S ... is OK. 
/tmp/.X11-unix/X0 ... file could not be opened. 
/tmp/tmp.LCZ9Ji6OLs ... is OK. 
/tmp/tmp.QdAt1TNQSH ... is OK. 
/tmp/ks-script-MqIN9F ... is OK. 
/tmp/tmp.mHXPvYeKjb/mcupgrade.conf ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uninstall-uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/mcscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/install-uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/readme.txt ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uvscan_secure ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/signlic.txt ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/liblnxfv.so.4 ... is OK. 

正しい出力が得られません。私はMULTILINEとDOTALLの両方のオプションを削除しようとしましたが、まだ正しい応答を得ていません。以下は、DOTALLとMULTILINEで実行したときの出力です。

9 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 

ご協力いただければ幸いです!ありがとう!!

+0

're.findall'を使用して、取得したすべての値を抽出します。 're.compile(r '^(?:(?!\ b(?:ファイルは開けませんでした|壊れました|ブロックです)\ b)。)+ $'、re.DOTALL | 're.findall'で –

+0

あなたが出力を対象とするものを表示できますか?即座に解決します。 –

+0

私は明らかにしていなかったと思います。私は走っているかのように、 "OK"か "壊れた"のようなもので終わらない行をすべて望みます:grep -v "OK" "$ {OUTDIR}/$ {OUTFILE} | grep -v "が壊れている" | grep -v "ファイルを開くことができませんでした" | grep -v "はブロック"> $ {OUTDIR}/$ {OUTFILE} .trimmed 2>&1 –

答えて

2

おそらく単純に考えて、ラインごと:

import re 
import sys 

pattern = re.compile(r"(is OK)|(file could not be opened)|(is a broken)|(is a block)") 

with open(sys.argv[1]) as handle: 
    for line in handle: 
     if not pattern.search(line): 
      sys.stdout.write(line) 

出力:

eth0 
10.0.11.196 
00:0C:29:AF:6A:A7 
parameters passed to uvscan: --DRIVER /opt/McAfee/uvscan/datfiles/current -- ANALYZE --AFC=32 ATIME-PRESERVE --PLAD --RPTALL RPTOBJECTS SUMMARY --UNZIP -- RECURSIVE --SHOWCOMP --MIME --THREADS=4 /tmp 
temp XML output is: /tmp/HIQZRq7t2R 
McAfee VirusScan Command Line for Linux64 Version: 6.0.5.614 
Copyright (C) 2014 McAfee, Inc. 
(408) 988-3832 LICENSED COPY - April 03 2016 

AV Engine version: 5700.7163 for Linux64. 
Dat set version: 8124 created Apr 3 2016 
Scanning for 670707 viruses, trojans and variants. 


No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/ATIME-PRESERVE 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/RPTOBJECTS 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/SUMMARY 
+0

私は彼がこれらの行を削除したいと言ったと思った。彼は他のすべてを望んでいる。 –

+0

ええ、私はあなたが 'pattern.search(line)がNone'(または' pattern.search(line) 'ではない場合)としたいと思います。 – Blckknght

+0

@CPandaは明らかに、コードが単純であれば簡単な修正です! – cdlane

0

は時々正規表現はもっと複雑ですが、あなたが本当に唯一のこれらのパターンを探しているならば、私はおそらくしたいです

terms = (
    'is OK', 
    'file could not be opened', 
    'is a broken', 
    'is a block', 
) 

with open('/tmp/sample.log') as f: 
    for line in f: 
     if line.strip() and not any(term in line for term in terms): 
      print(line, end='') 

これは正規表現よりも高速ではないかもしれませんが、それはほぼ単純ですそれが得られるようにe。また、あなたはまた、もう少し厳密なアプローチを使用することができます。私はかかるだろう

terms = (
    'is a broken', 
    'is a block', 
) 

with open('/tmp/samplelog.log') as f: 
    for line in f: 
     line = line.strip() 
     if not line: 
      continue 
     elif line.endswith('is OK.'): 
      continue 
     elif line.endswith('file could not be opened.'): 
      continue 
     elif any(term in line for term in terms): 
      continue 
     print(line) 

アプローチは、主に、私は、スクリプトを使用していると期待する人に依存します:)

0

これを試してみてください(と、それは1行で行われています)

p = re.compile("^(?:[if](?!s OK|s a broken|s a block|ile could not be opened)|[^if])*$") 

それはラインであなたが「I」または「F」を持っている場合、それはサフィックスが言及し続けることができないか、それはそれは大丈夫です「I」または「F」ではないということを意味します。これは、行のすべてのcharatersのためにそれを繰り返します。

編集:regex101.comでテストしたところ、なぜ動作していないのかがわかりました。以下は、動作する1行の正規表現です。

p = re.compile("^(?:[^if\n]|[if](?!s OK|ile could not be openeds OK|s a broken|s a block|ile could not be opened))*$", re.MULTILINE) 
+0

私はあなたの解決策を試しましたが、運はありません。私は何が間違っているのかよくわかりません... –

+0

このパターンにはいくつの結果がありますか? –

+0

私はそれが '(?:s OK | sa ...'が好きではないと思う。 –

関連する問題