gpt4 book ai didi

linux - 循环的 shell 脚本断行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:46 33 4
gpt4 key购买 nike

我正在尝试使用 cut 命令在另一个变量(如 ip 和端口号)中保存 $i 变量的一些数据,以便我可以将 ip 和端口都存储到数据库中,但是由于下一行,此输出会导致保存数据的问题,请帮助..

foo=( $(grep logs  data.txt) ) 

for i in "${foo[@]}"
do

echo "$i" | sed 's/Failed//g' | sed "s/logs//g" | sed "s/for//g" | sed "s/delmum//g" | sed "s/from//g" | sed "s/port//g" | sed "s/invalid//g" | sed "s/user//g"| sed "s/castis//g" | sed "s/guest//g" | sed '/^$/d'

done

Output :-Mar
4
03:08:15
sshd[96487]:
225.33.58.96
62445

Mar
4
03:08:15
sshd[65741]:
225.33.58.96
62445

Mar
4
03:08:15
sshd[34595]:
202.83.52.11
43321

Mar
4
03:08:16
sshd[25485]:
356.214.857.246
12445

Mar
4
03:08:16
sshd[25245]:
324.684.723.857
24875


output expected :-
Mar 4 03:08:15 sshd[96487]: 225.33.58.96 62445
Mar 4 03:08:15 sshd[34595]: 202.83.52.11 43321
Mar 4 03:08:16 sshd[25245]: 324.684.723.857 24875

data.txt
Mar 4 03:08:15 delmum sshd[96487]: Failed logs for root from 225.33.58.96 port 62445 ssh2
Mar 4 03:08:06 perfmum sshd[33799]: Connection closed by
Mar 4 03:08:15 delmum sshd[65741]: Failed logs for root from 225.33.58.96 port 62445 ssh2
Mar 4 03:08:15 delmum sshd[34595]: Failed logs for root from 202.83.52.11 port 43321 ssh2
Mar 4 03:08:06 delmum sshd[12485]: Connection closed by
Mar 4 03:08:06 delmum sshd[85468]: Connection closed by
Mar 4 03:08:06 delmum sshd[51396]: Connection closed by
Mar 4 03:08:16 delmum sshd[25485]: Failed logs for invalid user castis from 356.214.857.246 port 12445 ssh2
Mar 4 03:08:16 delmum sshd[25245]: Failed logs for invalid user castis from 324.684.723.857 port 24875 ssh2
Mar 4 03:08:06 delmum sshd[23541]: Connection closed by

data.txt 的内容是行的,但是为什么循环自己断行

最佳答案

使用单个 awk 命令:

awk '/logs/{ 
if (/from/ && /port/) { sub(/: .* from/,""); tail=":" OFS $6 OFS $8 }
print $1,$2,$3,$4,$5 tail; tail="";
}' data.txt

示例输出:

Mar 4 03:08:15 delmum sshd[96487]: 225.33.58.96 62445
Mar 4 03:08:15 delmum sshd[65741]: 225.33.58.96 62445
Mar 4 03:08:15 delmum sshd[34595]: 202.83.52.11 43321
Mar 4 03:08:16 delmum sshd[25485]: 356.214.857.246 12445
Mar 4 03:08:16 delmum sshd[25245]: 324.684.723.857 24875

关于linux - 循环的 shell 脚本断行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49297531/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com