gpt4 book ai didi

linux - 使用 bash 清理输出文件的最佳方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:27 24 4
gpt4 key购买 nike

我想知道是否有人可以告诉我一个用 awk、grep、cut 等清理这个 txt 文件的好方法?也是最优雅的方式。我已经设法摆脱了相当多的垃圾(文件比这个大得多),但我对摆脱“/”并将它们添加到新的单独行感到不高兴。

这是我尝试的代码行

cat nmap_results |grep [A-Za-z0-9] |sort -u > New_results.txt

这是结果:

Host: 192.168.15.201 () Ports: 21/closed/tcp//ftp///, 22/closed/tcp//ssh///, 23/closed/tcp//telnet///, 25/closed/tcp//smtp///, 80/closed/tcp//http///, 110/closed/tcp//pop3///, 139/open/tcp//netbios-ssn///, 443/closed/tcp//https///, 445/open/tcp//microsoft-ds///, 3389/closed/tcp//ms-wbt-server///
Host: 192.168.15.201 () Status: Up
Host: 192.168.15.202 () Ports: 21/closed/tcp//ftp///, 22/filtered/tcp//ssh///, 23/closed/tcp//telnet///, 25/filtered/tcp//smtp///, 80/open/tcp//http///, 110/closed/tcp//pop3///, 139/closed/tcp//netbios-ssn///, 443/filtered/tcp//https///, 445/filtered/tcp//microsoft-ds///, 3389/closed/tcp//ms-wbt-server///
Host: 192.168.15.202 () Status: Up
Host: 192.168.15.205 () Ports: 21/open/tcp//ftp///, 22/closed/tcp//ssh///, 23/closed/tcp//telnet///, 25/closed/tcp//smtp///, 80/open/tcp//http///, 110/closed/tcp//pop3///, 139/open/tcp//netbios-ssn///, 443/open/tcp//https///, 445/open/tcp//microsoft-ds///, 3389/open/tcp//ms-wbt-server///
Host: 192.168.15.205 () Status: Up

谢谢韦兹

最佳答案

您可以将逗号替换为换行符,并将正斜杠替换为空格。结果更具可读性;

$ sed 's/,/\n/g' logfile.txt | sed 's/\// /g'
Host: 192.168.15.201 () Ports: 21 closed tcp ftp
22 closed tcp ssh
23 closed tcp telnet
25 closed tcp smtp
80 closed tcp http
110 closed tcp pop3
139 open tcp netbios-ssn
443 closed tcp https
445 open tcp microsoft-ds
3389 closed tcp ms-wbt-server Host: 192.168.15.201 () Status: Up Host: 192.168.15.202 () Ports: 21 closed tcp ftp
22 filtered tcp ssh
23 closed tcp telnet
25 filtered tcp smtp
80 open tcp http
110 closed tcp pop3
139 closed tcp netbios-ssn
443 filtered tcp https
445 filtered tcp microsoft-ds
3389 closed tcp ms-wbt-server Host: 192.168.15.202 () Status: Up Host: 192.168.15.205 () Ports: 21 open tcp ftp
22 closed tcp ssh
23 closed tcp telnet
25 closed tcp smtp
80 open tcp http
110 closed tcp pop3
139 open tcp netbios-ssn
443 open tcp https
445 open tcp microsoft-ds
3389 open tcp ms-wbt-server Host: 192.168.15.205 () Status: Up

关于linux - 使用 bash 清理输出文件的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24529350/

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