Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trim leading whitespace from fields in csv file in Linux

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading