2

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.

vikas ramnani
  • 155
  • 3
  • 4
  • 12
  • Sorry to add up in the problem, The values here can contain spaces also (For ex a value1 can be "blah blah blah"). But I would like to maitain those spaces, I just want to remove whitespaces between two values. – vikas ramnani Jun 27 '12 at 14:46
  • 1
    Then you need to provide more accurate test input. – Thor Jun 27 '12 at 14:57

5 Answers5

4
$ 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
vergenzt
  • 9,669
  • 4
  • 40
  • 47
3

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
unwind
  • 391,730
  • 64
  • 469
  • 606
1

This will replace multiple spaces with just one space:

sed -r 's/\s+/ /g'
amaksr
  • 7,555
  • 2
  • 16
  • 17
0

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.

twmb
  • 1,710
  • 1
  • 20
  • 18
0

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

utopman
  • 581
  • 4
  • 13