简体   繁体   中英

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.

如果值本身始终没有空格,那么规范的解决方案(在我看来)将使用tr

$ tr -d '[:blank:]' < CSV_FILE > CSV_FILE_TRIMMED
$ 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

这将用一个空格代替多个空格:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM