从 bash 中的 CSV 文件的列中删除空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11228691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:37:09  来源:igfitidea点击:

Removing spaces from columns of a CSV file in bash

bash

提问by vikas ramnani

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.

我有一个 CSV 文件,其中每一列在实际值之后都包含不必要的空格(或制表符)。我想创建一个新的 CSV 文件,使用 bash 删除所有空格。

For example

例如

One line in input CSV file

输入 CSV 文件中的一行

abc def pqr             ;valueXYZ              ;value PQR              ;value4

same line in output csv file should be

输出 csv 文件中的同一行应该是

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 ?

我尝试使用 awk 来修剪每一列,但没有用。任何人都可以帮我解决这个问题吗?

Thanks in advance :)

提前致谢 :)

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

我编辑了我的测试用例,因为这里的值可以包含空格。

回答by vergenzt

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

$ cat cvs_file | awk 'BEGIN{ FS=" *;"; OFS=";" } {=; print 
$ tr -d '[:blank:]' < CSV_FILE > CSV_FILE_TRIMMED
}' abc def pqr;valueXYZ;value PQR;value4
}'
  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=$1is necessary to refresh $0.
  4. Print $0.
  1. 将输入字段分隔符 ( FS)设置为零个或多个空格后跟分号的正则表达式。
  2. 将输出字段分隔符 ( OFS) 设置为简单的分号。
  3. $1=$1有必要刷新$0
  4. 打印$0


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

回答by unwind

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

如果值本身总是没有空格,则规范的解决方案(在我看来)将使用tr

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

回答by amaksr

This will replace multiple spaces with just one space:

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

##代码##

回答by twmb

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'

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.

否则,如果您知道您的字段中不会出现多个空格,那么您可以使用 user1464130 给您的内容。

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

如果这不能解决您的问题,请回复我。

回答by utopman

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 :

我找到了一种方法来做我想做的事情,即以有效的方式删除空行并删除文件的尾随换行符。我这样做:

##代码##

from Remove blank lines with grep

用 grep 删除空行