2012-07-13 114 views
0

我有一個包含多個列和行的CSV [File1.csv]。從特定列匹配輸入文件的CSV中刪除行

我有另一個CSV文件(只有一列),列出一個特定的單詞[File2.csv]。

如果任何列匹配File2中列出的任何單詞,我希望能夠刪除File1中的行。

我最初使用這樣的:

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

這個工作,在一定程度上。我遇到的這個問題是列中有多個字(例如word1,word2,word3)。 File2包含word2,但並未刪除該行。

我累了把這些單詞分開看起來像這樣:(word1,word2,word3),但是原來的命令不起作用。

如何從File2中刪除包含單詞的行,並可能包含其他單詞?

回答

0

您可以在File2.csv中轉換包含多個圖案的分割線。

以下使用tr在將它們用作模式之前,將包含word1,word2的行轉換爲單獨的行。所述<()構建臨時充當文件/ FIFO(在bash測試):採用awk

grep -v -F -f <(tr ',' '\n' < File2.csv) File1.csv > File3.csv 
+0

所以我嘗試了你的方法,我仍然留下了與'grep -v -F -f File2.csv File1.csv> File3.csv' – eloscurosecreto 2012-07-13 17:27:59

+0

相同的結果然後你需要向我們展示'File1.csv的確切樣本'和'File2.csv'。以上工作與你迄今爲止提供的內容一致。 – Thor 2012-07-13 17:42:35

+0

以下是指向這些文件的鏈接: - [File1.csv](https://www.dropbox.com/s/ryrk0ofenzzmfuj/File1.csv) - [Files2.csv](https:// www。 dropbox.com/s/o59t2lfobgjugd5/File2.csv) 我希望這有助於。謝謝! – eloscurosecreto 2012-07-13 17:58:50

1

一種方式。

內容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 
} 

假設File1.csvFile2.csv已經托爾的答案的評論提供的內容(我建議加上這些信息的問題),運行像腳本:

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

With following output:

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

我收到錯誤'awk:1:意外字符'。'' – eloscurosecreto 2012-07-13 17:17:01

+0

@eloscurosecreto:你是直接從命令行運行嗎?我的意思是,不使用文件粘貼它。 – Birei 2012-07-13 17:25:43

+0

我創建了一個.awk文件,就是這個問題。我只是直接從cli運行你的代碼,並沒有任何問題完成,但是,我發現使用remove文件檢查了輸出,發現行中應該刪除的多個單詞實例。這種方法似乎只刪除了列中包含單個單詞的行(不包括「,word2,word3」)。 – eloscurosecreto 2012-07-13 17:43:25