Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove leading and trailing whitespace from all columns but one in a CSV?

Tags:

regex

perl

I have a CSV that looks like this:

things,ID,hello_field,more things
stuff,123  ,hello ,more stuff
stuff,123 ,hello ,more stuff
stuff ,123  ,hello ,more stuff
stuff,123  ,hello ,more stuff
stuff ,123,hello ,more stuff
stuff,123,hello ,more stuff
stuff ,123,hello ,more stuff

How can I remove leading and trailing whitespace from all columns except for the second (ID)? The final output would look like this:

things,ID,hello_field,more things
stuff,123  ,hello,more stuff
stuff,123 ,hello,more stuff
stuff,123  ,hello,more stuff
stuff,123  ,hello,more stuff
stuff,123,hello,more stuff
stuff,123,hello,more stuff
stuff,123,hello,more stuff

I tried using the following regex, but it removes spaces from all fields, including those in the ID column.

s/( +,|, +)/,/gi;
like image 435
JDE876 Avatar asked Dec 31 '25 08:12

JDE876


1 Answers

Split, trim selectively, rejoin

perl -F, -lane 's/^\s+|\s+$//g for @F[0,2..$#F]; print join ",", @F' file.csv

Explanation:

Switches:

  • -F/pattern/: split() pattern for -a switch (//'s are optional)
  • -l: Enable line ending processing
  • -a: Splits the line on space and loads them in an array @F
  • -n: Creates a while(<>){...} loop for each line in your input file.
  • -e: Tells perl to execute the code on command line.

Code:

  • EXPR for @F[0,2..$#F]: Iterate over array slice (skipping 2nd field)
  • s/^\s+|\s+$//g: Remove leading and trailing spaces from fields
  • print join ",", @F: Print the results
like image 157
Miller Avatar answered Jan 02 '26 00:01

Miller