Search code examples
awksedremoving-whitespace

Modify CSV file using sed or any other command


I am trying to remove spaces and tabs from csv file. (In Linux via command line)

sed "s/[[:blank:]]\{1,\}//g" ./TRIM.csv

By using the above command, the space between value/item of a column is removed.

(Eg. Hello World is becoming HelloWorld)

Also I want to replace '|' with '~'

SAMPLE FILE:

   0 |         null |               0 |              0 |             0 |             null |  null |
   1 |         Hello World |               0 |              0 |             0 |             null |  null |

Desired output :

0~null~0~0~0~null~null~
1~Hello World~0~0~0~null~null~

All spaces have been removed except the space between 'hello world' which is value of column that has space.


Solution

  •  sed -r 's/[[:space:]]+\|/~/g;s/~[[:space:]]+/~/g' file
    

    Enable regular expression interpretation with -r or -E and then replace one or more spaces and then "|" with "~". After this, replace "~" followed by one or more spaces with "~"