CodexBloom - Programming Q&A Platform

Parsing Custom Delimited Log Files in Ruby - Handling Inconsistent Quoting

๐Ÿ‘€ Views: 420 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-25
ruby csv parsing regex Ruby

I'm wondering if anyone has experience with I'm converting an old project and I'm trying to figure out I'm learning this framework and I'm attempting to parse a custom delimited log file in Ruby where fields are separated by semicolons and some fields can contain commas that are enclosed in quotes... Hereโ€™s a sample of the log file: ``` info;"User, John";2023-01-01;"Action performed" warn;"User, Jane";2023-01-02;"Failed login attempt" info;"User, Bob";2023-01-03;"Action performed" ``` The scenario I'm working with is that some fields can have quoted strings that contain commas, but other fields do not. I initially tried using the `CSV` class in Ruby with a custom separator, but it doesn't seem to handle the inconsistent quoting well. Hereโ€™s what Iโ€™ve tried: ```ruby require 'csv' log_data = File.read('logs.txt') rows = CSV.parse(log_data, col_sep: ';', quote_char: '"') rows.each do |row| puts row.inspect end ``` When I run this code, I get unexpected splitting like this: ``` "User, John" "Action performed" ``` instead of getting the full entries like "User, John" as a single field. I believe the scenario stems from the internal handling of quotes and commas within the CSV parser. Iโ€™ve tried various configurations, but nothing seems to yield the expected results. Is there a better way to approach this parsing task in Ruby? Would using a regex be a more effective solution, or is there a library that can better handle this format? Any guidance would be greatly appreciated! The project is a mobile app built with Ruby. Any pointers in the right direction? I'm working on a service that needs to handle this. What's the best practice here?