Reputation: 13
I have a txt file that I've used to capture data packets over my network. I'm wondering if anyone would be able to tell me how I can use regular expressions to search for all instances of 'IP len ='
in the file, and extract them into another file perhaps?
Upvotes: 1
Views: 76
Reputation: 64167
This should do the trick:
File.open("log.txt", 'r') do |f|
File.open("ip_len_out.txt", 'w') do |out|
f.lines.each do |line|
out.puts $1 if line.match(/IP\s+len\s*=(\d+)/)
end
end
end
ip_len_out.txt
now contains all the ip lengths, new line delimited.
Upvotes: 0
Reputation: 5259
This little script will do the trick (amend as you like):
#!/usr/bin/ruby -w
File.open('infile.txt', 'r') do |infile|
File.open('outfile.txt', 'w') do |outfile|
while (line = infile.gets) do
if line =~ /IP\s+len\s+=/ then
outfile.puts line
end
end
end
end
Upvotes: 2