简体   繁体   中英

How can i echo the newline after everyline

My host is Red Hat Enterprise Linux Server release 5.4 with bash.
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}' ,than it output the following:

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 ,it becomes the following:

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 . You can simplify your command to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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