Reputation: 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.
Upvotes: 0
Views: 205
Reputation: 66873
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 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.
Upvotes: 4
Reputation: 22012
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.
Upvotes: 0
Reputation: 2687
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.
Upvotes: 0