tags:

views:

96

answers:

1

i want to ignore ALL

TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,

and

Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,

from

TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,
Add,10.1.3.1,43172
Add,10.1.3.1,44172
Add,10.1.3.1,4172
Add,10.1.3.1,432
Add,10.1.3.1,435472
Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,
A: 

Given the contents of the CSV file:

s = "TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,
Add,10.1.3.1,43172
Add,10.1.3.1,44172
Add,10.1.3.1,4172
Add,10.1.3.1,432
Add,10.1.3.1,435472
Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,
"

gsub will remove the offending lines:

s = s.gsub(/TopLevel, Result,,,,,,,,\nAddHost,10.1.3.1,,,,,,,,\n/, '')
s = s.gsub(/Flush,,,,,,,,,\nAddHost,10.1.3.4,,,,,,,,\n/, '')
puts s
# => Add,10.1.3.1,43172
# => Add,10.1.3.1,44172
# => Add,10.1.3.1,4172
# => Add,10.1.3.1,432
# => Add,10.1.3.1,435472

You may want regular expressions less specific than that. For example, if you want to remove ANY AddHost line following a TopLevel line, then the first regexp would be:

/TopLevel, Result,,,,,,,,\nAddHost.*\n/

When gsub is done munching on the file, pass the results to the CSV parser as normal.

Wayne Conrad
Or, even better, run the CSV parser *first* and then operate on the parsed data.
Antal S-Z
Hi Wayne, Thanks for your response, i am new to Ruby , and trying to use #!/usr/bin/rubyrequire 'rubygems'require 'faster_csv' FasterCSV.foreach("HOUND-1.csv", :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row| puts row[0] break endnot sure HOW i woudl use the regex and substrings to ignore the offending lines
kamal