Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 执行ss命令后,cut命令出现故障_Linux_Bash_Sockets_Ss - Fatal编程技术网

Linux 执行ss命令后,cut命令出现故障

Linux 执行ss命令后,cut命令出现故障,linux,bash,sockets,ss,Linux,Bash,Sockets,Ss,以下是我使用的基本剪切语法: [jay@webserver ~]$ ss -tn State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 52

以下是我使用的基本剪切语法:

[jay@webserver ~]$ ss -tn
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
ESTAB      0      52                                      xxx.xx.xx.xx:xx                                                  xxx.xx.xx.xx:xx              
ESTAB      0      232                                     xxx.xx.xx.xx:xx                                                 xxx.xx.xx.xx:xx     
但是,当我尝试在字段上剪切时,我没有得到适当的输出:

[jay@webserver ~]$ ss -tn | grep -v State | cut -d$'\t' -f3,4
ESTAB      0      36     xxx.xx.xx.xx:xx                 xxx.xx.xx.xx:xx              
ESTAB      0      68     xxx.xx.xx.xx:xx                 xxx.xx.xx.xx:xx  

我能想到的唯一一件事是delimter不是一个选项卡,但是在这种情况下,我如何才能得到我想要的输出?

ss的输出用空格分隔。我建议使用awk:

ss -tn | grep -v State | awk '{print $3,$4}'

如果您不介意使用
awk
,您可以使用(这也节省了对grep的调用):


改为使用
awk
,默认情况下,它将一个或多个空白字符计数为单个分隔符:

ss -tn | grep -v State | awk '{print $3,$4}'
此外,这里还有一种更通用的跳过第一行的方法:

ss -tn | tail -n+2 | awk '{print $3,$4}'
这在我的机器上工作。 基本上,将8个空格中的每一个改为tab,删除其他空格

ss -tn | unexpand -t 8 | tr $'\t' '|' |tr -d [:blank:] | tr '|' $'\t' | expand -t 1 | cut -f4- -d ' '
或 这个:

ss -tn | expand -t 1 | unexpand -t 8 | cut -f4- -d$'\t'

呵呵,我们都说了同样的话。我想赛勒斯第一个打到了。
ss -tn | expand -t 1 | unexpand -t 8 | cut -f4- -d$'\t'