Home > Blockchain >  How do I remove spaces at the end of each line in my CSV file using PERL?
How do I remove spaces at the end of each line in my CSV file using PERL?

Time:11-10

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 REGEX in perl. Could you help me please? Best regards, Marc

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]
  • Related