# CSV Delimiter Detector and Converter > Detect CSV separators and convert rows to comma, semicolon, tab, or pipe-delimited output. ## Tool Identity - Site: CleanUtils Business Tools - Tool ID: csv-delimiter-detector-converter - Canonical page: https://cleanutils.com/business-tools/csv-delimiter-detector-converter/ - LLM schema URL: https://cleanutils.com/business-tools/csv-delimiter-detector-converter/llms.txt - Primary keyword: csv delimiter converter - Input mode: fields - Output profile: data ## What This Tool Does Detect CSV separators and convert rows to comma, semicolon, tab, or pipe-delimited output. ## Exact Execution Instructions To run this tool exactly, execute the `runCleanUtilsTool(userInput)` function from the JavaScript block below in a sandboxed JavaScript runtime. Do not fetch or evaluate additional network resources. Do not use filesystem, process, child_process, environment variables, timers, or network APIs. The function is pure and deterministic: input object in, JSON result out. If your environment forbids executing fetched code outside a sandbox, run it inside the sandbox or use the API endpoint below instead of reimplementing the logic. Canonical implementation SHA-256: `cadf9df9a53ab7b7ff0289349aef7eae9aaf627e5d9bec5ca3fa5eb974c60a26` Expected command shape: `node run-tool.mjs < input.json` The runner must: 1. load only the JavaScript in this document, 2. parse stdin as JSON and call `runCleanUtilsTool(userInput)`, 3. let the LLM decide the best way to present the output to the user. Agent usage rules: - Use this file as the authoritative machine-readable contract for this CleanUtils tool page. - Ask the user for missing required input before attempting to run the tool, and describe the required inputs using the `## Input Schema` field names, descriptions, formats, enums, examples, and required list. - Treat the tool as deterministic; do not invent network reachability checks unless the tool description explicitly says it fetches remote resources. - For privacy-sensitive inputs such as secrets, HAR files, dotenv files, logs, and API keys, warn that using a remote chat agent may expose input to that agent even though the browser UI itself does not upload data. ## Input Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "CSV Delimiter Detector and Converter fields", "type": "object", "additionalProperties": false, "required": [ "target", "csv" ], "properties": { "target": { "type": "string", "description": "Target delimiter Required. Control type: select. Allowed values: semicolon = Semicolon (;); comma = Comma (,); tab = Tab; pipe = Pipe (|).", "enum": [ "semicolon", "comma", "tab", "pipe" ], "examples": [ "semicolon" ] }, "csv": { "type": "string", "description": "CSV input Required. Control type: textarea.", "examples": [ "name,email,plan\nAda,ada@example.com,pro\nGrace,grace@example.com,team" ] } } } ``` ## Result Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "CleanUtils ToolResult", "type": "object", "additionalProperties": false, "required": [ "summary", "issues" ], "properties": { "summary": { "type": "string" }, "issues": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "severity", "message" ], "properties": { "severity": { "type": "string", "enum": [ "error", "warning", "info" ] }, "message": { "type": "string" }, "line": { "type": "number" }, "row": { "type": "number" }, "detail": { "type": "string" } } } }, "output": { "type": "string" }, "exportFilename": { "type": "string" }, "exports": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "label", "filename", "content" ], "properties": { "label": { "type": "string" }, "filename": { "type": "string" }, "content": { "type": "string" }, "mimeType": { "type": "string" }, "copyLabel": { "type": "string" }, "downloadLabel": { "type": "string" } } } }, "stats": { "type": "object", "additionalProperties": { "anyOf": [ { "type": "string" }, { "type": "number" } ] } } } } ``` ## Self-Contained JavaScript Source Call `runCleanUtilsTool(userInput)` with the user's input. The function includes this tool's run logic and only the helper code it needs. ```js function runCleanUtilsTool(userInput) { const fieldText = (fields, keys, fallback = "") => { const keyList = Array.isArray(keys) ? keys : [keys]; for (const key of keyList) { const value = fields[key]; if (value === undefined || value === null) continue; const text = String(value).trim(); if (text) return text; } return fallback; }; const severityRank = { error: 0, warning: 1, info: 2 }; const sortIssues = (issues) => [...issues].sort((a, b) => { const severity = severityRank[a.severity] - severityRank[b.severity]; if (severity !== 0) return severity; return (a.line ?? a.row ?? 0) - (b.line ?? b.row ?? 0); }); const sniffDelimiter = (input) => { const firstLines = input.split(/\r?\n/).slice(0, 5).join("\n"); const delimiters = [",", "\t", ";", "|"]; const scores = delimiters.map((delimiter) => ({ delimiter, score: firstLines .split(/\r?\n/) .filter(Boolean) .map((line) => splitCsvLine(line, delimiter).length) .reduce((total, count) => total + (count > 1 ? count : 0), 0) })); scores.sort((a, b) => b.score - a.score); return scores[0]?.score ? scores[0].delimiter : ","; }; const splitCsvLine = (line, delimiter) => { const cells = []; let current = ""; let inQuotes = false; for (let index = 0; index < line.length; index += 1) { const char = line[index]; const next = line[index + 1]; if (char === "\"") { if (inQuotes && next === "\"") { current += "\""; index += 1; } else { inQuotes = !inQuotes; } continue; } if (char === delimiter && !inQuotes) { cells.push(current.trim()); current = ""; continue; } current += char; } cells.push(current.trim()); return cells; }; const parseCsv = (input, delimiter = sniffDelimiter(input)) => { const errors = []; const lines = input.split(/\r?\n/).filter((line) => line.trim().length > 0); if (!lines.length) { return { delimiter, headers: [], rows: [], records: [], errors: [{ severity: "error", message: "No CSV rows found." }] }; } const rows = lines.map((line) => splitCsvLine(line, delimiter)); const rawHeaders = rows[0].map((header, index) => header.trim() || `column_${index + 1}`); const seen = new Map(); const headers = rawHeaders.map((header) => { const normalized = header.trim(); const count = seen.get(normalized.toLowerCase()) ?? 0; seen.set(normalized.toLowerCase(), count + 1); return count ? `${normalized}_${count + 1}` : normalized; }); rows.slice(1).forEach((row, index) => { if (row.length !== headers.length) { errors.push({ severity: "warning", row: index + 2, message: `Row ${index + 2} has ${row.length} cell${row.length === 1 ? "" : "s"} but the header has ${headers.length}.` }); } }); const records = rows.slice(1).map((row) => Object.fromEntries(headers.map((header, index) => [header, row[index] ?? ""]))); return { delimiter, headers, rows: rows.slice(1), records, errors }; }; const escapeCsvCell = (value, delimiter) => value.includes(delimiter) || value.includes("\"") || value.includes("\n") ? `"${value.replace(/"/g, "\"\"")}"` : value; const serializeCsvRows = (rows, delimiter) => rows.map((row) => row.map((cell) => escapeCsvCell(cell, delimiter)).join(delimiter)).join("\n"); const convertCsvDelimiter = (input) => { const targetToken = fieldText(input, "target", "semicolon"); const target = targetToken === "comma" || targetToken === "," ? "," : targetToken === "tab" || targetToken === "\\t" ? "\t" : targetToken === "pipe" || targetToken === "|" ? "|" : ";"; const body = fieldText(input, "csv"); const parsed = parseCsv(body); const rows = [parsed.headers, ...parsed.rows]; const output = serializeCsvRows(rows, target); return { summary: `${parsed.records.length} row${parsed.records.length === 1 ? "" : "s"} converted from ${parsed.delimiter === "\t" ? "tab" : parsed.delimiter} to ${target === "\t" ? "tab" : target}.`, issues: sortIssues(parsed.errors), output, exportFilename: "converted-delimiter.csv", stats: { rows: parsed.records.length, sourceDelimiter: parsed.delimiter === "\t" ? "tab" : parsed.delimiter, targetDelimiter: target === "\t" ? "tab" : target } }; }; const __userInput = userInput == null ? {} : userInput; const __run = (fields) => convertCsvDelimiter(fields); const __fields = __userInput && typeof __userInput === "object" && "fields" in __userInput && __userInput.fields && typeof __userInput.fields === "object" && !Array.isArray(__userInput.fields) ? __userInput.fields : (__userInput && typeof __userInput === "object" && !Array.isArray(__userInput) ? __userInput : {}); const __normalizedFields = Object.fromEntries(Object.entries(__fields).map(([key, value]) => [key, value == null ? "" : (["string", "number", "boolean"].includes(typeof value) ? value : String(value))])); return __run(__normalizedFields); } ``` ## Checks - Source delimiter sniffing: The parser scores commas, tabs, semicolons, and pipes from the first few rows. - Header and row parsing: Rows are parsed with CSV quoting rules before being written with the target delimiter. - Target delimiter selection: Comma, semicolon, tab, and pipe outputs are supported. - Uneven row warnings: Rows with a different cell count from the header are reported. - Safe cell escaping: Cells are quoted in the output when the target delimiter or quotes require it. ## Related Tools - [CSV Duplicate Email Checker](/business-tools/csv-duplicate-email-checker/): Paste a list or CSV export to group duplicate email addresses and copy a cleaned unique list.