如何在Apache NiFi中移除CRLF?CSV列错位修复求助
Hey there, let's tackle this CRLF issue that's messing up your CSV columns. The root problem here is that unintended CRLF line breaks are showing up inside your CSV fields instead of only at the end of each row. That's why your data is shifting to the next line and breaking the column structure. Here's exactly how to fix it with NiFi's Replace Text processor:
Step 1: Set Up and Configure the Replace Text Processor
Drag the Replace Text processor onto your NiFi canvas and connect it to your CSV source (like GetFile or ListFile). Then configure these key parameters:
1.1 Define the Search Value (Regex)
We need to target only the CRLFs that are inside fields, not the legitimate ones at the end of rows. The regex you use depends on whether your CSV uses quotes to wrap fields:
If your CSV doesn't use quote-wrapped fields (like your example):
Use this regex:(?<!^)\r\n
What it does: The(?<!^)is a negative lookbehind that ensures we only match CRLFs that aren't at the start of a line. This skips valid row-ending CRLFs and only hits the ones messing up your fields.If your CSV uses double quotes for field wrapping (standard for most CSVs with multi-line fields):
Use this regex:\r\n(?=(?:[^"]*"[^"]*")*[^"]*$)
What it does: This targets CRLFs that aren't enclosed within a pair of double quotes. It preserves intentional multi-line fields while removing rogue CRLFs that cause column shifts.
1.2 Set the Replacement Value
Choose what to replace the problematic CRLFs with:
- If you just want to eliminate the line break entirely, use a single space (
) — this keeps field content intact without breaking the row structure. - If you need to retain line break semantic, you can replace with
\n(LF only), but make sure your downstream tools support this. A space is usually the safest bet.
1.3 Tweak Other Critical Settings
- Replacement Strategy: Select
Replace Allto fix every instance of rogue CRLFs, not just the first one. - Search Mode: Pick
Regex Replacesince we're using regular expressions to target the right line breaks. - Character Set: Match the encoding of your CSV file (e.g.,
UTF-8) to avoid garbled text after processing.
Step 2: Verify the Fix
Start the processor, then check the output. You can use NiFi's Data Provenance view to inspect the processed flow file, or write it to a directory and open it in Notepad++ to confirm columns are no longer misaligned and unwanted CRLFs are gone.
Extra Tips
- If the regex isn't matching correctly, use NiFi's built-in regex tester (click the question mark next to the Search Value field) to test it against your sample CSV content. This helps you tweak the regex until it hits exactly the CRLFs you need to replace.
- If your CSV is mostly standard but has a few rogue line breaks, you could also use
CSVReaderfollowed byCSVRecordSetWriterto re-serialize the CSV properly. But this works best if the CSV uses quote-wrapped fields; for unquoted CSVs with random line breaks, Replace Text pre-processing is still your best bet.
内容的提问来源于stack exchange,提问作者Keshav Balivada

