Home > OS >  Trim leading whitespace from fields in csv file in Linux
Trim leading whitespace from fields in csv file in Linux

Time:12-06

I want to trim only the leading whitespaces from a csv file that looks something like this:

thing1,thing2, thing something3,thing4
thing11,thing12, thing something13,thing14
etc.

(note: I want to keep the space between thing and something3)

so my output would be like this:

thing1,thing2,thing something3,thing4
thing11,thing12,thing something13,thing14

I've tried using awk '{$1=$1};1' file and a dozen other solutions that I found with a quick google search, but those usually either freeze or just do nothing in my case.

Hope someone can help me out, cheers!

CodePudding user response:

sed 's/,[[:blank:]]*/,/g' file

Example:

> cat test.txt 
thing1,thing2, thing something3,thing4
thing12,thing12, thing something13,thing14

> cat test.txt  | sed 's/,[[:blank:]]*/,/g'
thing1,thing2,thing something3,thing4
thing12,thing12,thing something13,thing14

CodePudding user response:

With:

cat file
thing1,thing2, thing something3,thing4
thing12,thing12, thing something13,thing14

In awk, you can deal with non-quoted csv and remove the trailing space like so:

awk 'BEGIN{FS=",[[:space:]]*"; OFS=","}
{$1=$1}1' file 

Prints:

thing1,thing2,thing something3,thing4
thing11,thing12,thing something13,thing14

Suppose you have quoted csv and you only want to change the csv delimiters -- not the embedded commas in the quoted fields, you should use a csv parser.

Given:

cat file
thing1,thing2, thing something3,"thing4, and 5"
thing11,thing12, thing something13,thing14

The easiest csv parser at the command line is ruby:

ruby -r CSV -e 'CSV.parse($<.read).each{|l| puts l.map(&:lstrip).to_csv}' file

Prints:

thing1,thing2,thing something3,"thing4, and 5"
thing11,thing12,thing something13,thing14

CodePudding user response:

Also with awk you can test this:

awk 'gsub(/, */, ",") 1' file
thing1,thing2,thing something3,thing4
thing12,thing12,thing something13,thing14
  • Related