Search IP from a text file in .csv log file, if fo

2019-06-14 15:32发布

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

There are 2 problems with this:

  1. It creates SUSPICIOUS column on every row

  2. It creates new column on left side instead of right side

(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.

1条回答
欢心
2楼-- · 2019-06-14 15:53

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

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

查看更多
登录 后发表回答