I have file called regional.txt and it has data as below :
shell.SetMyFile "Ranger"
shell.SetMyErrorFile "Discovery"
shell.SetMyFileEnabled 1
shell.SetMyLogFileEnabled 1
Now I am reading this file using ruby and trying to filter the text from "shell.SetMyFile" and "shell.SetMyErrorFile" as Ranger and Discovery:
File.readlines("regional.txt").each do |line|
value1 = line.split(" ")[1] if line.include?("shell.SetMyFile")
value2 = line.split(" ")[1] if line.include?("shell.SetMyErrorFile ")
puts value1, value2
end
My result is 1 ,1 rather than Ranger and Discovery. This is because include? method considering the "shell.SetMyFileEnabled" and "shell.SetMyLogFileEnabled". How can I filter it to the desired result ?
A quick fix would be as follows:
You could do it with regex as well:
To explain the regex, consider
shell.SetMyFile "Ranger":\w+: any number of alphaneumeric chars, matches the prefix e.g. shell\.: a literal period(\w+): match group 1, any number of alphaneumeric chars, matches the suffix e.g. SetMyFile[^\w]+any number of non-alphaneumeric chars, matches whitespace as well as quotation(\w+)match group 2, any number of alphaneumeric chars. matches the value string e.g. RangerAfter calling
scanyou are left with a nested array:You can call
to_hon this to turn it into a hash, for easy lookup by key: