簡體   English   中英

來自文件和輸出模式的Grep(如果找不到)

[英]Grep from a file and output pattern if not found

我有一個文件ids_list.csv ,其中包含一個ID列表,每行一個。

我還有一個日志文件,我想在其中找到ids_list.txt的id。

我想要的是將日志文件中的行(如果找到該模式)打印到result.txt文件,否則將其打印。

所以我寫了這個腳本:

#!/bin/bash

for i in `cat ids_list.csv`;
  do
    echo $i
    echo `grep $i log_FVAScope`
    if [[ ! res=$(grep $i log_FVAScope) ]]; then
      echo $i >> result.txt;
    else
      echo $res >> result.txt
    fi
  done

但是, result.txt為空,我在做什么錯?

而且它似乎很慢,我如何加快速度( ids_list.csv包含約40k行,日志文件包含70萬行)?

編輯:樣本輸入:

ids_list.csv

KBKEQO17564
SPXTCT769178
KBKFXS1952894
CDNEVL_4148105
BBR10000130794156

日志文件

18:51:59.368 [pool-1-thread-4] INFO  c.s.m.x.liqor.filter.CodChainFilter - KBKEQO17564 excluded by CodeChain Filter
18:51:59.369 [pool-1-thread-5] INFO  c.s.m.x.liqor.filter.CodChainFilter - KBKFXS1952894 excluded by CodeChain Filter
18:51:59.369 [main] INFO  c.s.m.x.l.manager.FilterManagerImpl - waiting new deals to submit
18:51:59.369 [pool-1-thread-2] INFO  c.s.m.x.liqor.filter.CodChainFilter - CDNEVL_4148105 excluded by CodeChain Filter
18:51:59.369 [pool-1-thread-1] INFO  c.s.m.x.liqor.filter.CodChainFilter - BBR10000130794156 excluded by CodeChain Filter

所需的輸出( result.txt

18:51:59.368 [pool-1-thread-4] INFO  c.s.m.x.liqor.filter.CodChainFilter - KBKEQO17564 excluded by CodeChain Filter
SPXTCT769178
18:51:59.369 [pool-1-thread-5] INFO  c.s.m.x.liqor.filter.CodChainFilter - KBKFXS1952894 excluded by CodeChain Filter
18:51:59.369 [main] INFO  c.s.m.x.l.manager.FilterManagerImpl - waiting new deals to submit
18:51:59.369 [pool-1-thread-2] INFO  c.s.m.x.liqor.filter.CodChainFilter - CDNEVL_4148105 excluded by CodeChain Filter
18:51:59.369 [pool-1-thread-1] INFO  c.s.m.x.liqor.filter.CodChainFilter - BBR10000130794156 excluded by CodeChain Filter

好吧,您實際上不需要腳本。 下面的命令可以滿足您的需求。

while read line; do grep $line log_FVAScope >> result.txt ; done < ids_list.csv

我不知道性能問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM