require 'csv' require 'date' start_date = Date.today - (364) end_date = Date.today random_date = Date.jd(rand(start_date.ajd..end_date.ajd)) formatted_date = random_date.strftime('%m%d') puts formatted_date input_file = 'tmp/BBSTPA_Med_Accum_20260218.txt' output_file = 'tmp/sample_elig_file.csv' captured_groups = [] # Read input, process, and write output CSV.open(output_file, 'w', col_sep: '|') do |csv_out| headers = CSV.foreach(input_file, col_sep: '|').first csv_out << headers CSV.foreach(input_file, headers: true, col_sep: '|').with_index do |row, i| if row['Carrier Code'] == "CHAA" && !captured_groups.include?(row['Group Number']) random_date = Date.jd(rand(start_date.ajd..end_date.ajd)) formatted_date = random_date.strftime('%m%d').to_s dob = row['Date of Birth'].to_s.gsub(/.{4}$/, "#{formatted_date}") row['Last Name'] = 'DOE' row['First Name'] = 'JANE' row['Date of Birth'] = dob row['Social Security Number'] = '#########' row['Subscriber Social Security Number'] = '#########' captured_groups << row['Group Number'] csv_out << row end end end