从.csv日志文件中的文本文件中搜索IP(如果找到),在其旁边添加新列 [英] Search IP from a text file in .csv log file, if found add new column next to it

查看:91
本文介绍了从.csv日志文件中的文本文件中搜索IP(如果找到),在其旁边添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个Shell脚本,该脚本从.csv日志文件内的文本文件中查找IP地址,如果找到结果,它将在csv文件的右侧添加一个新列,称为"SUSPICIOUS" "

I need a shell script that finds IP addresses from a text file inside a .csv log file, and if it found the results, it'll make a new column on the right side of the csv file, calling it "SUSPICIOUS"

到目前为止,我想出了以下命令:

So far, I came up with this command:

cat *.csv | grep --color=always -z -w -f 'list.txt' | sed 's/^/SUSPICIOUS, /' > *.csv

这有2个问题:

  1. 它在每一行上创建SUSPICIOUS列

  1. It creates SUSPICIOUS column on every row

它将在左侧而不是右侧创建新列

It creates new column on left side instead of right side

(如果我使用正则grep表达式,则只会保存包含这些IP地址的结果,我需要日志的每一行)

(If I use regular grep expression, it'll only save the results containing those IP addresses, I need every single row of the logs)

非常感谢您抽出宝贵的时间阅读本文并提供帮助.

Thanks in advanced for taking your time to read this and helping out.

推荐答案

使用awk似乎更容易.将文本文件加载到关联数组的键中.然后读取CSV文件,并测试数组中是否包含IP字段.

This seems like it would be much easier to do with awk. Load the text file into the keys of an associative array. Then read the CSV file, and test whether the IP field is contained in the array.

awk -i inplace -F, -v OFS=, '
    NR==FNR { a[$0]++; next } # Put lines from list.txt into array
    a[$8] { print $0 "SUSPICIOUS" } # Test if IP in current row is in array
    { print }' inplace=0 list.txt inplace=1 *.csv # Otherwise print row normally

这使用GNU awk的inplace扩展名,因此它可以将结果写回到输入文件中.

This uses the inplace extension of GNU awk so it can write the result back to the input file.

这篇关于从.csv日志文件中的文本文件中搜索IP(如果找到),在其旁边添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆