ruby - How do I split command output by space or tab? -
i'm trying do:
input = %x{netstat -ano | grep ^:80} input.gsub(/\s+\t/m,' ').strip.split(" ") puts input[4]
but output of: c
, expected <pid>
example output of netstat command is:
tcp 0.0.0.0:8080 0.0.0.0:0 listening 1800 tcp 0.0.0.0:8081 0.0.0.0:0 listening 8780 tcp 0.0.0.0:8085 0.0.0.0:0 listening 5540
additionally, i'm unsure of how make grep
match exactly 80
(or other port specify).
a plain split without arguments want:
input.lines.map |line| proto, local, foreign, state, pid = line.split pid end #=> ["1800", "8780", "5540"]
you code has several problems want point out though, maybe can learn this:
- you using
gsub
,strip
never changesinput
. might want usegsub!
,strip!
(mutators) purpose.split
has no mutator equivalent, because return value array , not string. input[4]
gives fourth character ofinput
string
Comments
Post a Comment