regex - How to split outputs of single column with awk? -
i want grep out port , ip address netstat -tulpn
command. output :
active internet connections (only servers) proto recv-q send-q local address foreign address state tcp 0 0 127.0.0.1:3010 0.0.0.0:* listen tcp 0 0 127.0.0.1:3306 0.0.0.0:* listen tcp 0 0 127.0.1.1:5334 0.0.0.0:* listen tcp 0 0 127.0.0.1:6113 0.0.0.0:* listen tcp6 0 0 ::1:601 :::* listen udp 0 0 0.0.0.0:5013 0.0.0.0:* udp 0 0 127.0.1.1:5333 0.0.0.0:* udp 0 0 0.0.0.0:6341 0.0.0.0:* udp 0 0 0.0.0.0:53553 0.0.0.0:* udp 0 0 0.0.0.0:5123 0.0.0.0:* udp6 0 0 :::4905 :::* udp6 0 0 :::353 :::*
with awk
local address column.
sudo netstat -tulpn | awk '{ print $4;}'
now want split port , ip each other , show in 2 separate columns. what's simplest way it? (regex or somthing that)
you want split on last colon in field. gnu awk:
gawk '{match($4, /(.*):(.*)/, m); print m[1], m[2]}'
or
gawk '{print gensub(/(.*):/,"\\1 ", 1, $4)}'
with non-gnu awk:
mawk '{ip=$4; sub(/:[^:]+/,"",ip); port=$4; sub(/.*:/,"",port); print ip,port}'
with perl
perl -lape '$_ = join(" ", $f[3] =~ /(.*):(.*)/)'
pipe output | column -t
make pretty.
Comments
Post a Comment