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 changes input. might want use gsub! , strip! (mutators) purpose. split has no mutator equivalent, because return value array , not string.
  • input[4] gives fourth character of input string

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -