微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重新输出打印

在几个不同的linux机器上,我必须计算不同端口的tcp套接字连接数量和状态。 最后打印输出可能看起来像这样。

49570 10.10.10.10:13062 ESTABLISHED 783 10.10.10.10:18080 CLOSE_WAIT 493 10.10.10.10:18082 CLOSE_WAIT 109 10.10.10.10:18080 SYN_RECV 17 10.10.10.10:15062 TIME_WAIT 15 10.10.10.10:15062 ESTABLISHED

第一列是count,第二个是ip:port,第三个是status。

我想要做的是重新格式化输出,以便它像这样出来

13062 15062 18080 18082 ESTABLISHED 49570 15 0 0 CLOSE_WAIT 0 0 783 493 SYN_RECV 0 0 109 0 TIME_WAIT 0 17 0 0

ip不同于机器,可能有更多的端口,或更多的状态,或更less。 用awk来实现这个可能吗? 有没有人有如何得到这个例子。

有什么工具可以自动格式化Ruby / Rails代码

shell中的Tab字符给予不同的宽度

Par Paragraph Formatter – 如何在Windows命令行中使用它

在C中打印hex的前导零

免费本地(不在线)HTML格式化软件的Windows

对不起,我很难将所需的input/输出结果粘贴,如上所述。 非常感谢。

格式化sqlPlus中查询输出

以编程方式创build可启动的NTFS / FAT分区Windows XP到Windows 7

使用* nix中的列命令格式化列表

我可以设置小数点符号在我的应用程序中到处使用

如何将输出与屏幕中心alignment – C ++?

$ cat tst.awk { sub(/.*:/,"",$2) ports[$2] statuses[$3] counts[$2,$3] = $1 for (i=1;i<=NF;i++) { maxWidth[i] = (length($i) > maxWidth[i] ? length($i) : maxWidth[i]) } } END { statusWidth = maxWidth[3] otherWidth = (maxWidth[1] > maxWidth[2] ? maxWidth[1] : maxWidth[2]) + 2 printf "%-*s",statusWidth,"" for (port in ports) { printf "%*s",otherWidth,port } print "" for (status in statuses) { printf "%-*s",status for (port in ports) { printf "%*d",counts[port,status] } print "" } } $ awk -f tst.awk file 13062 15062 18080 18082 SYN_RECV 0 0 109 0 CLOSE_WAIT 0 0 783 493 ESTABLISHED 49570 15 0 0 TIME_WAIT 0 17 0 0

这是另一个awk解决方

awk -F"[: ]+" 'BEGIN{delim="t"} {ports[$3]; status[$3][$4]=$1; st[$4]} END{str=delim for(key in ports){str=str""delim""key} print str for(k in st){ str =k for (key in ports) { str = str""delim""(status[key][k] ? status[key][k] : 0) } print str } }' test.txt

输出(如果你想用空白而不是“ t”作为分隔符,把delim="t"改为delim=" " ):

15062 13062 18080 18082 SYN_RECV 0 0 109 0 CLOSE_WAIT 0 0 783 493 ESTABLISHED 15 49570 0 0 TIME_WAIT 17 0 0 0

对于这个输入(即没有前导空格的地方):

49570 10.10.10.10:13062 ESTABLISHED 783 10.10.10.10:18080 CLOSE_WAIT 493 10.10.10.10:18082 CLOSE_WAIT 109 10.10.10.10:18080 SYN_RECV 17 10.10.10.10:15062 TIME_WAIT 15 10.10.10.10:15062 ESTABLISHED

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐