Ask Your Question
1

how to compare facts with all other facts in a text file

asked 2014-12-11 09:40:55 -0500

supriya gravatar image

updated 2014-12-11 09:46:34 -0500

Hi,

I am trying to compare my existing facts using text file. But it is giving me wrong answer.

Here is my facts

require 'rexml/document'
require 'facter/util/file_read'
require 'facter'

logfile = "/home/weblogic/javacheck.txt"
java_count = 0
log = Facter::Util::FileRead.read(logfile)
unless log.nil?
    log.each_line do |line|
    if line.include?('/java')
     java_count += 1
   val = "#{line}"
Facter.add(:isjavaversion) do
setcode do
        if Facter.value(:java_foundversion2_path2) == #{val}
               true
        else
               false
        end
end
end
end
end
end

Here is my text file

1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/bin/java

javafounfversion2path fact mactches to the text file. But it is returning false statement. It is not reading line by line when i do using irb statement. it is taking as entire string.

"1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java\n1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java\n1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/bin/java\n

How to split these lines by line?

edit retag flag offensive close merge delete

Comments

What is the value of the java_foundversion2_path2 fact? I'm not sure what you mean by it "matches to the text file". Also, it would help to know what you are trying to determine about your system with this custom fact. Could there be a simpler way to achieve what you want?

GregLarkin gravatar imageGregLarkin ( 2014-12-11 20:26:36 -0500 )edit

If you want to solve the problem facing you with your existing code, are you saying that the Facter::Util::FileRead.read(logfile) call returns a single string as shown above? If so, it looks like you can split on the \n characters to form an array of lines, then iterate on that.

GregLarkin gravatar imageGregLarkin ( 2014-12-11 20:28:16 -0500 )edit

Implement the code change described in the answer below, and you should be all set.

GregLarkin gravatar imageGregLarkin ( 2014-12-12 12:25:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-12-11 10:48:22 -0500

rilindo gravatar image

Since Facter::Util::FileRead.read slurps the file into a single string, your best option is to split log using the return character as a delimiter:

log.split('\n')

This will return the result as an array, which you can iterate.

irb(main):009:0> log="1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java\n1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java\n1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/bin/java\n"
=> "1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java\n1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java\n1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java\n1.7.0_72/u01/java/jdk1.7.0_72/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java\n1.7.0_65/u01/java/jdk1.7.0_65/bin/java\n"
irb(main):010:0> log.split('\n').each do | e |
irb(main):011:1* puts e
irb(main):012:1> end
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/jre/bin/java
1.7.0_72/u01/java/jdk1.7.0_72/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/jre/bin/java
1.7.0_65/u01/java/jdk1.7.0_65/bin/java
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-12-11 09:40:55 -0500

Seen: 182 times

Last updated: Dec 11 '14