vikas ramnani
vikas ramnani

Reputation: 155

Removing spaces from columns of a CSV file in bash

I have a CSV file in which every column contains unnecessary spaces(or tabs) after the actual value. I want to create a new CSV file removing all the spaces using bash.

For example

One line in input CSV file

abc def pqr             ;valueXYZ              ;value PQR              ;value4

same line in output csv file should be

abc def pqr;valueXYZ;value PQR;value4

I tried using awk to trim each column but it didnt work. Can anyone please help me on this ?

Thanks in advance :)

I edited my test case, since the values here can contain spaces.

Upvotes: 2

Views: 12805

Answers (5)

utopman
utopman

Reputation: 601

I found one way to do what I wanted that is remove blank line and remove trailing newline of a file in an efficient way. I do this with :

grep -v -e '^[[:space:]]*$' foo.txt

from Remove blank lines with grep

Upvotes: 0

vergenzt
vergenzt

Reputation: 10367

$ cat cvs_file | awk 'BEGIN{ FS=" *;"; OFS=";" } {$1=$1; print $0}'
  1. Set the input field separator (FS) to the regex of zero or more spaces followed by a semicolon.
  2. Set the output field separator (OFS) to a simple semicolon.
  3. $1=$1 is necessary to refresh $0.
  4. Print $0.

$ cat cvs_file
abc def pqr             ;valueXYZ              ;value PQR              ;value4

$ cat cvs_file | awk 'BEGIN{ FS=" *;"; OFS=";" } {$1=$1; print $0}'
abc def pqr;valueXYZ;value PQR;value4

Upvotes: 4

twmb
twmb

Reputation: 1810

If you know what your column data will end in, then this is a surefire way to do it:

sed 's|\(.*[a-zA-Z0-9]\) *|\1|g'

The character class would be where you put whatever your data will end in.

Otherwise, if you know more than one space is not going to come in your fields, then you could use what user1464130 gave you.

If this doesn't solve your problem, then get back to me.

Upvotes: 0

amaksr
amaksr

Reputation: 7745

This will replace multiple spaces with just one space:

sed -r 's/\s+/ /g'

Upvotes: 1

unwind
unwind

Reputation: 399753

If the values themselves are always free of spaces, the canonical solution (in my view) would be to use tr:

$ tr -d '[:blank:]' < CSV_FILE > CSV_FILE_TRIMMED

Upvotes: 3

Related Questions