From: Michael Randall Date: 2009-12-06T09:37:17+09:00 Subject: CSV confusion newbie question I am confused on why my two Ruby scripts using CSV behave differently. I hope someone can explain why it works this way and how to get consistent results. My first attempt worked; I parsed a CSV file with double-quoted, comma separated input, and sent it via SMTP to our Ticketing System, and it worked perfectly. The CSV handling section looks like this, (line numbers help show wrapped lines): 37 38 # Open csv input file Read-only, and assign each line to variables, 39 # then call email, in turn. 40 41 CSV.open(csvfile, 'r') do |row| 42 qid,cvss_base,cvss_temporal,cve_id,cve_url,bugtraq_id,bugtraq_url,vendor_id,vendor_url,last_update,category,vuln _type,title,diagnosis,solution,consequence,pci_flag,qualys_severity = row 43 subject = "QID #{qid} #{title}" The values were used as in this method: 27 serverport = 25 28 Net::SMTP.start( mailserver, serverport ) do |smtp| 29 smtp.open_message_stream( sourceaddr, destaddr ) do |f| 30 f.puts "From: #{sourceaddr}" 31 f.puts "To: #{destaddr}" 32 f.puts "Subject: #{subject}" 33 f.puts body 34 end 35 end 36 end Now I wanted to take another CSV file, (a list from our Systems Database), containing IP addresses, search code (usually a simple hostname), one field contains a list which may have IPs, Netmasks, and other data. When I tried matching the first IP field, it failed, and upon puts'ing the line I found it was still quoted! Next is the entire (failing) script: 1 #!/usr/bin/ruby -w 2 # 3 # @(#) Usage: parsetest.rb [input.csv] 4 # 5 # 6 7 require 'csv' 8 require 'net/smtp' 9 10 unless ARGV.length == 1 11 puts "Usage: #{0} csv_input_file" 12 exit 13 end 14 15 # Assign first argument to variable "csvfile" 16 csvfile = ARGV[0] 17 18 CSV.open(csvfile, 'r') do |row| 19 mgmtip,searchcode,cid,status,ipaddr,otherip,ciowner,description,category,loccat,locparent,loc,rack = row 20 #if mgmtip =~ /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/ 21 puts "Search code: #{searchcode}, \n\tMgmt_IP: #{mgmtip}, CID: #{cid}, #{status}, #{ciowner}" 22 #end 23 24 end This is partial output from the script: $ parsetest.rb testexport.csv Search code: "Search code", Mgmt_IP: "Management IP", CID: "ID", "CI Owner Organization Search code", "Other IP Addresses" Search code: "OLD-ENV01", Mgmt_IP: "10.192.72.82", CID: "206", "SEESN", "192.168.1.2" Search code: "OLD-ENV02", Mgmt_IP: , CID: "207", "SEESN", Search code: "OLD-PUB01C01", Thanks for any help, tips, explanation. 7thWAP, total newbie -- Posted via http://www.ruby-forum.com/.