How to Compare CSV Files by ID Without Line-by-Line Confusion
When CSVs are reordered, line-by-line diffs fail. Use business IDs to track changes. This guide shows how to manually compare files and highlights tools that automate the process.
Why this is harder than it looks
CSV files often get reordered during exports, making line-by-line comparisons useless. Manual methods require sorting by ID, handling duplicates, and accounting for data tolerances, which is error-prone without the right tools.
Step by step
- Identify the ID column. Locate the column(s) that uniquely identify rows, like 'customer_id' or 'product_code'. Ensure both files use the same ID structure and naming. For composite IDs, select multiple columns in the same order in both files.
- Standardize file structure. Verify both CSVs have the same delimiter (comma, tab, etc.), header rows, and encoding (UTF-8). Remove extra spaces, BOM markers, or hidden characters that might disrupt parsing. Use a text editor with hex view to check for anomalies.
- Sort both files by ID. In a spreadsheet tool like Excel or Google Sheets, sort both files by the ID column. For large files, use command-line tools like 'sort -k1,1' in Linux or PowerShell's Sort-Object. Ensure sorting is case-insensitive if IDs mix upper/lowercase.
- Compare row by row. Open both sorted files side-by-side. Match IDs and check for discrepancies in other columns. Use conditional formatting in spreadsheets to highlight differences in non-ID columns. Note rows present in one file but not the other.
- Apply data tolerances. For numeric columns, allow small differences (e.g., 0.1% variance). For dates, ignore time zones or format variations. For text, trim whitespace and ignore case. Use formulas like ABS((A1-B1)/A1) < 0.001 to flag numeric outliers.
- Export and validate. Save the comparison as a new CSV. Use a diff tool like WinMerge or Beyond Compare to verify changes. Check that added/removed rows match expected counts. Validate 5-10 random rows manually to confirm accuracy.
- Document findings. Create a summary of changes, including ID lists for added/removed rows and field-level differences. Use a spreadsheet to categorize issues (e.g., 'price mismatch', 'duplicate ID'). Share this with stakeholders for review.
Common mistakes
- Ignoring case sensitivity in ID comparisons
- Failing to remove duplicate IDs before sorting
- Not accounting for decimal locale differences
- Overlooking BOM markers in CSV headers
Where CSV Reconcile fits in
CSV Reconcile is a JetBrains IDE plugin that compares two CSV files by a column you choose as the ID. It runs locally on your files, so nothing is uploaded, and reports rows that were added, removed or changed between the two exports.
FAQ
Ensure both files have the same column order and names. Use a tool to map headers if they differ, or manually adjust one file to match the other before comparing.
The plugin flags duplicate IDs and lets you set policies (first/last/error). Manually review duplicates to determine if they’re valid or data errors.
Free version limits to 1,000 rows. Paid plans remove this cap and add advanced tolerances. For manual methods, split large files into smaller batches for comparison.