When viewing comma-delimited text files or CSV files in a text editor, the columns won’t align unless the fields were padded with spaces to make their widths equal. Other applications may treat any whitespace as significant, requiring an unpadded CSV file. With PowerGREP, you can easily pad and unpad CSV files.
To pad CSV files, we’ll search using a regular expression that matches one CSV field. As the replacement, we’ll re-insert the regex match padded with spaces to a specific length. Padding is easy in PowerGREP with match placeholders.
You can find this action in the PowerGREP5.pgl standard library as “CSV: Pad fields”.
To remove the padding from the CSV file, you can use the exact same action. Only the replacement text will be different. The regular expression uses a capturing group to store the actual text of the CSV field, separate from the whitespace at the start of the field that the overall regular expression also matches. So to remove the padding, all we need to do is to replace the overall regex match with the text matched by the capturing group. You can do this with the placeholder %GROUP1% or simply the backreference $1.
You can find this action in the PowerGREP5.pgl standard library as “CSV: Unpad fields”.