0

複数の列と行を含むCSV[File1.csv]があります。

特定の単語[File2.csv]をリストした別のCSVファイル(1列のみ)があります。

File2にリストされている単語のいずれかに一致する列がある場合、File1内の行を削除できるようにしたい。

私はもともとこれを使用しました:

 grep -v -F -f File2.csv File1.csv > File3.csv

これはある程度機能しました。私が遭遇したこの問題は、単語以上の列(word1、word2、word3など)で発生しました。File2にはword2が含まれていましたが、その行は削除されませんでした。

(word1、word2、word3)のように単語を分散させるのに疲れましたが、元のコマンドは機能しませんでした。

File2から単語を含み、他の単語が含まれている可能性のある行を削除するにはどうすればよいですか?

4

2 に答える 2

1

を使用した片道awk

の内容script.awk:

BEGIN {
    ## Split line with a doble quote surrounded with spaces.
    FS = "[ ]*\"[ ]*"
}

## File with words, save them in a hash.
FNR == NR {
    words[ $2 ] = 1;
    next;
}

## File with multiple columns.
FNR < NR {
    ## Omit line if eigth field has no interesting value or is first line of
    ## the file (header).
    if ( $8 == "N/A" || FNR == 1 ) {
        print $0
        next
    }

    ## Split interested field with commas. Traverse it searching for a
    ## word saved from first file. Print line only if not found.

    ## Change due to an error pointed out in comments.
    ##--> split( $8, array, /[ ]*,[ ]*/ )
    ##--> for ( i = 1; i <= length( array ); i++ ) {
    len = split( $8, array, /[ ]*,[ ]*/ )
    for ( i = 1; i <= len; i++ ) {
    ## END change.

        if ( array[ i ] in words ) {
            found = 1
            break
        }
    }
    if ( ! found ) {
        print $0
    }
    found = 0
}

Thorの回答のコメントにコンテンツが提供されていると仮定File1.csvして(その情報を質問に追加することをお勧めします)、スクリプトを次のように実行します。File2.csv

awk -f script.awk File2.csv File1.csv

次の出力で:

"DNSName","IP","OS","CVE","Name","Risk"
"ex.example.com","1.2.3.4","Linux","N/A","HTTP 1.1 Protocol Detected","Information"
"ex.example.com","1.2.3.4","Linux","CVE-2011-3048","LibPNG Memory Corruption Vulnerability (20120329) - RHEL5","High"
"ex.example.com","1.2.3.4","Linux","CVE-2012-2141","Net-SNMP Denial of Service (Zero-Day) - RHEL5","Medium"
"ex.example.com","1.2.3.4","Linux","N/A","Web Application index.php?s=-badrow Detected","High"
"ex.example.com","1.2.3.4","Linux","CVE-1999-0662","Apache HTTPD Server Version Out Of Date","High"
"ex.example.com","1.2.3.4","Linux","CVE-1999-0662","PHP Unsupported Version Detected","High"
"ex.example.com","1.2.3.4","Linux","N/A","HBSS Common Management Agent - UNIX/Linux","High"
于 2012-07-13T16:54:13.640 に答える
0

で複数のパターンを含む分割線を変換できますFile2.csv

以下は、パターンとして使用する前に、tr含む行を別の行に変換するために使用します。word1,word2コンストラクトは、<()一時的にファイル/FIFO として機能します ( でテスト済みbash)。

grep -v -F -f <(tr ',' '\n' < File2.csv) File1.csv > File3.csv
于 2012-07-13T16:53:27.950 に答える