File1.log:
Result for scripts as follows {"Script 1"=>{"expected"=>"Pass", "actual"=>"Pass", "result"=>true},
"Script 2"=>{"expected"=>"Fail", "actual"=>"Pass", "result"=>false}
-----
-----
-----
-----
Result for scripts as follows {"Script 3"=>{"expected"=>"Pass", "actual"=>"Pass", "result"=>true}, "Script 4"=>{"expected"=>"Fail", "actual"=>"Pass", "result"=>false}
File2.log:
Result for scripts as follows {"Script 1"=>{"expected"=>"Pass", "actual"=>"Pass", "result"=>true}, "Script 2"=>{"expected"=>"Pass", "actual"=>"Pass", "result"=>true}
I will have to parse the .log files which will contain multiple lines and get the above given lines alone.
The only thing common to be used to get this is the first text: Result for scripts as follows.
Any suggestions to solve this using Ruby or any powershell commands?
In short, you need to traverse each log file line by line, perform a pattern match and print the results.
The below code should solve your problem
# my log files are in my current working directory
log_files = Dir["./*.log"]
false_matches = []
log_files.each do |x|
puts "\n#{x}\n==========\n"
f = File.open(x,"r")
f.each_line do |line|
false_matches.push(line.scan(/"Script \d*"=>{"expected"=>".{4}", "actual"=>".{4}", "result"=>false/))
end
puts "\n"
f.close()
end
false_matches.each do |x|
unless x.length == 0
puts x[0].scan(/Script \d*/)
end
end
Related
I have a simple function to read lines from .txt file:
function loadData(file_name, root_path)
-- here, file_name is './list.txt', path is '../data/my/'
for line in io.lines(file_name) do
local data = {}
base_path = root_path .. line
-- so, base_path is something like ../data/my/001
data.file = base_path .. '_color.png'
print(data)
end
end
I expect the data should be {file: "../data/my/001_color.png"}, but I got {_color.png" ../data/my/001}
Can anyone help me? Thanks!
Check your ./list.txt file content for EOL (end of line) as it may be produced on windows (EOL=CR LF) an interpreted on linux (EOL=LF). io.lines takes CR character into line string on linux!
Your programm makes everything correct, but your data is not.
Let assume your first line in ./list.txt is ../data/my/001\r\n
line variable is ../data/my/001\r (print(#line) gives 15 instead of 14 ).
Carriage return (CR) in print moves cursor to start line position witout changing line.
Your print output in this case is something simmilar to {file: "../data/my/001\r_color.png"} (as it depends on print implementation) and you get output:
{file: "../data/my/001
_color.png"} <-- on the same line
Let's combine it:
_color.png"}ata/my/001
To correct this:
provide file without CR (works correctly on all systems)
add in loop on first row: line = line:gsub('[\r\n]','') to remove CR LF
Within our file structure we have snippets with tags. From my project directory, I want to return the snippet name based on the tag declared on the preceding line, for all files ending in .feature within the project.
For example, the directory contains a login.feature file with the following text:
#integration #unit #smoke
Scenario: Login to Product
#integration #unit
Scenario: Buy Stuff
The directory also contains a logout.feature file with the following text:
#test #wip #smoke
Scenario: Logout of Product
I want to run a single command that returns the following array:
["Login to Product", "Logout of Product"]
As you can see, I'm essentially doing the following:
grep -inr -A 1 '#smoke' ./my/directory | grep Scenario | sed -n -e 's/^.*Scenario: //p' | awk '!seen[$0]++'
...with the exception of converting the resulting lines into an array.
Is there an elegant way to do this in Ruby? Or can I somehow combine the Unix magic with Ruby sugar? Or should I stick to sed/awk/grep?
Here's what I came up with
results = []
Dir.glob("#{folder}/**/*.feature").each do |file|
i = 0; line = nil
File.foreach(file).map{ |l| line = i+1 if l[/#smoke/]; i+=1 }
results << IO.readlines(file)[line].gsub(/Scenario:\s|\n/,'') unless line.nil?
end
p results
The Results:
["Login to Product", "Logout of Product"]
You can easily shift this into a method.
Edit: Updated this after re-examining your full command line. Looks like you only want the Scenario that comes AFTER a #smoke line and no other Scenarios should be included in the output.
I am trying to import a file using ruby and parse it. Is there a way to read the next line once inside the file import? Basically I want to see if a specific line is within x lines of another important line. Like does "x phrase" Come within 10 lines of "y phrase". I don't see a way to do this -- I know its simple with Java.
Thanks!
You can also try:
web_contents = "c:\\path\\to\\your\\file.txt"
File.open(web_contents).each_with_index do |line, i|
line.chomp!
puts "line #{line}, i #{i}" # Do whatever you want to here
end
The .each_with_index method gives you an index, i, which you can use to keep track of where on what line in your file you are. Simple maths can then yield the offset as required.
To read lines of a file
lines_array = IO.readlines('testfile')
lines_array.each { |l| #Do your stuff with your line }
VoilĂ
Ruby Docs on IO
I am trying to recurse a directory, and all its subdirectories. I dont want to use "Find" or any other way except this one:
task :locate do
Dir.chdir(Dir.pwd+"/public/servers_info/config/deploy/")
puts "Current Directory is: "+ Dir.pwd
dir = Dir.pwd
def get_information(dir)
Dir.foreach(".") {|f|
next if f == '.' or f == '..'
if File.directory? f
puts f
#puts Dir.pwd+"/"+f
get_information(Dir.pwd+"/"+f)
else
puts "Not Directory"
end
}
end
get_information(dir)
end
I am pretty sure that it will work, I just dont know why it get stucks in the first directory! It enters the base directory, checks is the file is a directory or not, and then runs the SAME function again. But it doesnt! it gets stuck on the first folder and I get an error! Any help?
Your code is always looking at the "current" (.) directory. Your get_information method passes in a value bound to dir, which you never use.
Since you never use that parameter, you never change directories.
What you're trying to do is easier with Dir.glob, but if you're wedded to your solution, you'll need to change Dir.foreach(".") to something like Dir.foreach(dir).
Edited to add: If all you want is to print out a list of subdirectories, I would do
puts Dir.glob('*/**').select { |f| File.directory? f}
This includes only directories. If you want pretty close to the exact output of your existing code, I would do something like:
puts Dir.glob('*/**').map { |f| File.directory?(f)? f : "Not a Directory" }
Check out Dir.glob. Docs here
I'm using the following code to try and get all files from ftp using Ruby.
files = ftp.list()
files.each do |file|
ftp.gettextfile(file)
end
The problem is ftp.list returns a whole line of information, not just the filename e.g.
-rw-r--r-- 1 ftp ftp 0 May 31 11:18 brett.txt
How do I extract the filname from this string?
Many thanks
You can use the nlst public method like this
files = ftp.nlst("*.zip")|ftp.nlst("*.txt")|ftp.nlst("*.xml")
#optionally exclude falsely matched files
exclude = /\.old|temp/
#exclude files with 'old' or 'temp' in the name
files = files.reject{ |e| exclude.match e } #remove files matching the exclude regex
files.each do |file|
#do something with each file here
end
If you want to process the output of ftp.list you may find net-ftp-list useful.
However, list appears to be useful, as you can pass in a matching pattern, which it doesn't appear that nlst supports. I just did a quick-and-dirty hack to make list output work:
ftp.list("*.zip") do |zipfile|
zipfile = zipfile.split(/\s+/).last
# ... do something with the file
end