Created
December 2, 2025 14:22
-
-
Save EinLama/455cd7ce3772755e4532138524f596fc to your computer and use it in GitHub Desktop.
Group CSV file by column name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require "csv" | |
| def print_help | |
| puts "Usage: ruby #{File.basename(__FILE__)} <column> <filename.csv>" | |
| puts "Please provide a column name and a CSV filename as an argument.\n" | |
| end | |
| def group_csv_by_column(filename, column) | |
| rows = CSV.read(filename, headers: true) | |
| unless rows.headers.include?(column) | |
| puts "Error: CSV file does not contain a '#{column}' column." | |
| exit | |
| end | |
| grouped = rows.group_by { |row| row[column] } | |
| CSV.generate do |csv| | |
| csv << rows.headers | |
| grouped.each do |_, group_rows| | |
| group_rows.each { |row| csv << row.fields } | |
| end | |
| end | |
| end | |
| if ARGV.size != 2 | |
| print_help | |
| else | |
| column = ARGV[0] | |
| filename = ARGV[1] | |
| begin | |
| puts group_csv_by_column(filename, column) | |
| rescue Errno::ENOENT | |
| puts "Error: File '#{filename}' not found." | |
| rescue StandardError => e | |
| puts "Error reading file: #{e.message}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment