0

I'm using Ruby 1.8.7. I have a text file with following content:

"testhost-01.test.de|lan|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|test|wato|/" + FOLDER_PATH + "/",
"testhost-02.test.de|lan|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|prod|puppetagent|wato|/" + FOLDER_PATH + "/",
"testhost-03.test.de|wan|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|prod|puppetagent|wato|/" + FOLDER_PATH + "/",
"testhost-04.test.de|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|dmz|prod|puppetagent|wato|/" + FOLDER_PATH + "/",
"testhost-05.test.de|wan|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|prod|puppetagent|wato|/" + FOLDER_PATH + "/",
"testhost-06.test.de|lan|ip-v4|cmk-agent|tcp|ip-v4-only|site:tir_projects|prod|wato|/" + FOLDER_PATH + "/",
"testhost-07.test.de|ip-v6|cmk-agent|tcp|site:tir_projects|ip-v6-only|dmz|prod|puppetagent|wato|/" + FOLDER_PATH + "/",
"testhost-08.test.de|ip-v4|snmp|snmp-only|ip-v4-only|critical|site:tir_projects|dmz|wato|/" + FOLDER_PATH + "/",

I'm trying to extract the hostnames (testhost-01.test.de - testhost-08.test.de) to an Array but only when "puppetagent" is in the same line.

The result should be:

[
  "testhost-02.test.de",
  "testhost-03.test.de",
  "testhost-04.test.de",
  "testhost-05.test.de",
  "testhost-07.test.de"
]

Code Example:

path = "Textfile"
file = IO.read(path)
nodes = file.scan(/^"(.*)\|lan.*\|puppetagent/).flatten 

This example above works only for lines where after the first pipe, "lan" follows, so it only finds host 02.

Kristján
  • 18,165
  • 5
  • 50
  • 62
mobios
  • 353
  • 1
  • 2
  • 7
  • many many thanks Kristján. It works very good. – mobios Jan 08 '16 at 20:01
  • I'd highly recommend moving from Ruby 1.8.7 ASAP. It's extremely out of date; Continuing to use it will only cause problems. Look at RVM and rbenv for how to manage multiple versions of Ruby on a system. – the Tin Man Jan 08 '16 at 21:27

1 Answers1

3

If you don't want to restrict output to lines that include |lan, you can't include |lan in the expression. It looks like you want |lan to mark the end of your capture group - instead, you can restrict your capture group to not include | by using the character set [^|]. Then, even if the line doesn't include lan, you'll stop at the first |. After the |, you don't care about content until puppetagent, so we'll consume that with .*.

/^"([^|]*).*puppetagent/

In plain English, that's

  • ^" Start with "
  • ([^|]*) Capture anything that's not a |
  • .* Accept anything else on the line
  • puppetagent Require puppetagent to be present
Kristján
  • 18,165
  • 5
  • 50
  • 62