简体   繁体   English

如何在每行之后回显换行符

[英]How can i echo the newline after everyline

My host is Red Hat Enterprise Linux Server release 5.4 with bash. 我的主机是带有bash的Red Hat Enterprise Linux Server release 5.4
When i use this command: netstat -tnpl | grep "tcp" | awk '{print $4}' 当我使用这个命令时: netstat -tnpl | grep "tcp" | awk '{print $4}' netstat -tnpl | grep "tcp" | awk '{print $4}' netstat -tnpl | grep "tcp" | awk '{print $4}' ,than it output the following: netstat -tnpl | grep "tcp" | awk '{print $4}' ,而不是输出以下内容:

127.0.0.1:2208
0.0.0.0:871
0.0.0.0:9001
0.0.0.0:3306
0.0.0.0:111
127.0.0.1:631
127.0.0.1:25
127.0.0.1:6010
127.0.0.1:6011
127.0.0.1:2207
:::80
:::22
:::8601
::1:6010
::1:6011
:::443  

But when use this: ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port 但是当使用它时: ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port ,it becomes the following: ip_port=`netstat -tnpl | grep "tcp" | awk '{print $4}'` && echo $ip_port ,它变成如下:

127.0.0.1:2208 0.0.0.0:871 0.0.0.0:9001 0.0.0.0:3306 0.0.0.0:111 127.0.0.1:631 127.0.0.1:25 127.0.0.1:6010 127.0.0.1:6011 127.0.0.1:2207 :::80 :::22 :::8601 ::1:6010 ::1:6011 :::443  

It becomes online. 它变成了在线。 How did this happen and what i want is the original format. 这是怎么发生的,我想要的是原始格式。

You need to enclose your variable in quotes when echoing: 在回显时,您需要将变量括在引号中:

echo "$ip_port"

Also, you don't need to use both grep and awk because awk can grep . 此外,您不需要同时使用grepawk因为awk可以grep You can simplify your command to: 您可以将命令简化为:

netstat -tnpl | awk '/tcp/{print $4}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM