【问题标题】:print differences of file1 to file2 without deleting anything from file2打印 file1 到 file2 的差异而不从 file2 中删除任何内容
【发布时间】:2019-01-31 19:45:19
【问题描述】:

我正在创建一个脚本,用于根据预定义的黑 IP 列表从 .csv 日志文件中搜索 IP。

它首先导入日志文件,然后从中解析IP,然后根据预定义的黑IP列表搜索解析的IP,最后它需要询问用户(如果找到任何结果)将结果保存到原始日志文件那是进口的。

文件 1 是代码中 IP-output.csv 的示例。

文件 2 是代码中 $filename 的示例(原始导入的 .csv)。

文件 1:

107.147.166.60 ,SUSPICIOUS IP
107.147.167.26 ,SUSPICIOUS IP
108.48.185.186 ,SUSPICIOUS IP
108.51.114.130 ,SUSPICIOUS IP
142.255.102.68 ,SUSPICIOUS IP

文件 2:

outlook.office365.com ,174.203.0.118 ,UserLoginFailed
outlook.office365.com ,107.147.166.60 ,UserLoginFailed
outlook.office365.com ,107.147.167.26 ,UserLoginFailed
outlook.office365.com ,174.205.17.24 ,UserLoginFailed
outlook.office365.com ,108.48.185.186 ,UserLoginFailed
outlook.office365.com ,174.226.15.21 ,UserLoginFailed
outlook.office365.com ,108.51.114.130 ,UserLoginFailed
outlook.office365.com ,67.180.23.93 ,UserLoginFailed
outlook.office365.com ,142.255.102.68 ,UserLoginFailed
outlook.office365.com ,164.106.75.235 ,UserLoginFailed

我想把文件 2 改成这样:

outlook.office365.com ,174.203.0.118 ,UserLoginFailed
outlook.office365.com ,107.147.166.60 ,UserLoginFailed ,SUSPICIOUS IP
outlook.office365.com ,107.147.167.26 ,UserLoginFailed ,SUSPICIOUS IP
outlook.office365.com ,174.205.17.24 ,UserLoginFailed
outlook.office365.com ,108.48.185.186 ,UserLoginFailed ,SUSPICIOUS IP
outlook.office365.com ,174.226.15.21 ,UserLoginFailed
outlook.office365.com ,108.51.114.130 ,UserLoginFailed ,SUSPICIOUS IP
outlook.office365.com ,67.180.23.93 ,UserLoginFailed
outlook.office365.com ,142.255.102.68 ,UserLoginFailed ,SUSPICIOUS IP
outlook.office365.com ,164.106.75.235 ,UserLoginFailed

这是我创建的脚本:

#!/bin/bash
#
# IP Blacklist Checker
#Import .csv (File within working directory)
echo "Please import a .csv log file to parse/search the IP(s) and UserAgents: "
read filename
#Parsing IPs from .csv log file
echo "Parsing IP(s) from imported log file..."
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' $filename | sort | uniq > IP-list.txt
echo 'Done'
awk 'END {print NR,"IP(s) Found in imported log file"}' IP-list.txt
echo 'IPs found in imported log file:'
cat IP-list.txt
#searches parsed ip's against blacked ip lists
echo 'Searching parsed IP(s) from pre-defined Blacked IP List Databases...'
fgrep -w -f "IP-list.txt" "IPlist.txt" > IP-output.txt
awk 'END {print NR,"IP(s) Found Blacked IP List Databases"}' IP-output.txt
echo 'Suspicious IPs found in Blacked IP List Databases:'
cat IP-output.txt
while true; do
read -p "Do you want to add results to log file?" yn
case $yn in
    [Yy]* ) grep -Ff IP-output.txt $filename | sed 's/$/ ,SUSPICIOUS IP/' > IP-output.csv && awk 'FNR==NR {m[$1]=$0; next} {for (i in m) {match($0,i); val=substr($0, RSTART, RLENGTH); if (val) {sub(val, m[i]); print; next}};} 1' IP-output.csv $filename > $filename; break;;
    [Nn]* ) break;;
    * ) echo "Please answer yes or no.";;
esac
done
echo "Finished searching parsed IP(s) from pre-defined Blacked IP List Databases."
rm IP-list.txt IP-output.csv IP-output.txt 

我正在导入的日志文件非常长,有 15-20 列,而 IPlist.txt(涂黑的 IP)中有超过 15000 个 IP。将结果保存到同一个日志文件后,.csv 文件为空,如果我以不同的名称保存它,所有列都会乱序,并且 IP 列旁边出现“,可疑 IP”列,我需要它而不是在最后一列(行尾)。

我也不知道如何仅在找到任何内容时提示保存文件,如果不仅提示未找到!

我得到的结果:

 outlook.office365.com ,174.203.0.118 ,UserLoginFailed
 outlook.office365.com ,107.147.166.60 ,SUSPICIOUS IP ,UserLoginFailed
 outlook.office365.com ,107.147.167.26 ,SUSPICIOUS IP ,UserLoginFailed
 outlook.office365.com ,174.205.17.24 ,UserLoginFailed
 outlook.office365.com ,108.48.185.186 ,SUSPICIOUS IP ,UserLoginFailed
 outlook.office365.com ,174.226.15.21 ,UserLoginFailed
 outlook.office365.com ,108.51.114.130 ,SUSPICIOUS IP ,UserLoginFailed
 outlook.office365.com ,67.180.23.93 ,UserLoginFailed
 outlook.office365.com ,142.255.102.68 ,SUSPICIOUS IP ,UserLoginFailed
 outlook.office365.com ,164.106.75.235 ,UserLoginFailed

【问题讨论】:

  • 欢迎来到 SO。 Stack Overflow 是一个面向专业和狂热程序员的问答网站。目标是您将一些自己的代码添加到您的问题中,以至少显示您为解决这个问题所做的研究工作。
  • 请避免"Give me the codez" 问题。而是显示您正在处理的脚本并说明问题所在。另见How much research effort is expected of Stack Overflow users?
  • 逗号是不是故意放反了?
  • 逗号表示 .csv 文件中的单独列。所以如果你在文本编辑器中打开一个 .csv 文件,每个逗号代表一列
  • @bruh321 如果答案解决了您的问题,请点击绿色✓。

标签: linux bash shell awk sed


【解决方案1】:

你的意思是这样的:

awk 'FNR==NR { m[$1]=$0; next; } { for (i in m) { idx = index($0, i); if (idx > 0) { print substr($0, 1, idx-1) m[i]; next; } } } 1' file1.txt file2.txt > newfile2.txt

它基本上按顺序处理file1.txtfile2.txtFNR==NR 对于第一个文件中的所有行都是 true,其中映射 m 是用替换模式构建的(第一个空格之前的所有内容都映射到整行)。对于第二个文件,将在m 中检查每一行是否匹配。如果匹配(使用index()),脚本会打印匹配前的所有内容,然后打印来自m 的值。哦,最后的 1 将打印 file2 中不匹配的行。

【讨论】:

  • 很高兴这个答案适用于 OP 但是,如果它包含对它正在做什么的解释,我会赞成它。
  • 我没有对你投反对票。看到问题下@jww 的评论了吗?每次他发表评论时,他也会对每个答案投反对票,因为他不喜欢这个问题。他以它而闻名,有时他会在不发表评论的情况下这样做。当我们看到该评论时,我和其他人会投票赞成每个答案以补偿(例如stackoverflow.com/a/52018840/1745001stackoverflow.com/questions/51862398/…),就像我现在为您所做的那样。这很荒谬,但版主说他没有违反任何规则,所以我们坚持下去。
  • @EdMorton data.stackexchange.com/stackoverflow/query/890812 :-) 有一个柜台+1。
  • @EdMorton 我直到最近才知道 jww,当时一位好心的用户向我指出了我收到的反对票。我支持“投票补偿”(当然,除非答案真的很糟糕)......所以在这里,伙计们,采取+1 :)
  • @EdMorton,我不知道这一点,但现在你已经指出了,我记得我也被 jww-ed 了!那个讨厌的流氓..
猜你喜欢
  • 1970-01-01
  • 2017-07-10
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2017-11-20
相关资源
最近更新 更多