Home > Back-end >  How do I remove spaces around each field in a CSV in Perl?
How do I remove spaces around each field in a CSV in Perl?

Time:11-11

My CSV files with whitespace:

Id ;   FirstName   ;   LastName  ;    email
123;    Marc       ;   TOTO      ;    [email protected] 

I would like delete whitespace in my csv by line like this :

Id;FirstName;LastName;email
123;Marc;TOTO;[email protected]

I would use a regex in Perl.

CodePudding user response:

It is always a good idea to use a library with file formats like CSV. Even as this case seems trivial and safe to parse with regex surprises can and do sneak up. Also, requirements tend only to change and projects and data only get more complex. Once there is sensible code using a good library a project evolution is generally far more easily absorbed.

A library like the excellent Text::CSV can use ; as a separator and can remove that extra whitespace while parsing the file, with a suitable option.

To keep it short and in a one-liner the functional interface is helpful

perl -MText::CSV=csv -we' 
    csv (in => *ARGV, sep => ";", allow_whitespace => 1)' name.csv > new.csv

Prints as desired with the supplied example file.

CodePudding user response:

Not a perl solution but the awk solution is so simple it might be acceptable:

awk '{OFS="";$1=$1;print $0}' file.csv;

This process uses OFS to override the default output field separator from the usual white space. $1=$1 forces awk to reset the whole line $0 value to remove the field separators before printing it.

CodePudding user response:

Although your title says remove spaces at the end of each line, you may want to remove whitespaces around the field values. Then would you please try:

perl -pe "s/\s*;\s*/;/g" input_file.csv

Output:

Id;FirstName;LastName;email
123;Marc;TOTO;[email protected]

Please note the code breaks in case the field contains ; itself such as abc;"foo ; bar";def or other complicated cases.

  • Related