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
.