bash remove everything before character
I have the following:
netstat -tuna | awk '{ print $4; }' | sed -e '1,2d'
which returns:
10.20.26.143:51697
10.20.26.143:51696
10.20.26.143:51698
10.20.26.143:51693
10.20.26.143:51695
10.20.26.143:51694
:::22
0.0.0.0:68
0.0.0.0:49625
0.0.0.0:5353
:::48727
:::5353
which returns a list of all open ports.. how would i remove everything before the : charac开发者_JS百科ter? please notice that a few ones have ::: instead of : i just want it to return the ports, remove all text before the last :
i want it all in one bash command.
thanks a lot in advance!
add to your sed-command:
';s/.*:/:/g'
netstat -tuna | awk '{ print $4; }' | sed -e '1,2d;s/.*:/:/g'
should work.
Just do it with one awk command. No need to chain too many
netstat -tuna | awk 'NR>2{ k=split($4,a,":");print a[k] }'
or
netstat -tuna | awk 'NR>2{ sub(/.*:/,"",$4);print $4 }'
or Ruby(1.9+)
netstat -tuna | ruby -ane 'puts $F[3].sub(/.*:/,"") if $.>2'
if you want unique ports,
netstat -tuna | awk 'NR>2{ sub(/.*:/,"",$4); uniq[$4] }END{ for(i in uniq) print i }'
So apparently, you can tuna network, but you can't tuna fish
The following will print all open ports
netstat -tuna | awk -F':+| +' 'NR>2{print $5}'
The following will print all unique open ports, i.e. no duplicates sorted in ascending order
netstat -tuna | awk -F':+| +' 'NR>2{print $5}' | sort -nu
精彩评论