如何使用sed用逗号替换空格?

时间:2022-03-28 16:48:55

I would like to replace the empty space between each and every field with comma delimiter.Could someone let me know how can I do this.I tried the below command but it doesn't work.thanks.

我想用逗号分隔符替换每个字段之间的空白区域。有人让我知道我该怎么做。我尝试了下面的命令,但它不起作用。谢谢。

My command:
:%s//,/


53 51097 310780 1
56 260 1925 1
68 51282 278770 1
77 46903 281485 1
82 475 2600 1
84 433 3395 1
96 212 1545 1
163 373819 1006375 1
204 36917 117195 1

5 个解决方案

#1


25  

If you are talking about sed, this works:

如果你在谈论sed,这有效:

sed -e "s/ /,/g" < a.txt

In vim, use same regex to replace:

在vim中,使用相同的正则表达式替换:

s/ /,/g

#2


7  

Inside vim, you want to type when in normal (command) mode:

在vim内部,您希望在正常(命令)模式下键入:

:%s/ /,/g

On the terminal prompt, you can use sed to perform this on a file:

在终端提示符下,您可以使用sed在文件上执行此操作:

sed -i 's/\ /,/g' input_file

Note: the -i option to sed means "in-place edit", as in that it will modify the input file.

注意:sed的-i选项意味着“就地编辑”,因为它将修改输入文件。

#3


2  

I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:

我知道这不是你要求的,但是,用换行替换逗号,这很有用:

tr , '\n' < file

#4


1  

Try the following command and it should work out for you.

尝试以下命令,它应该适合你。

sed "s/\s/,/g" orignalFive.csv > editedFinal.csv

#5


1  

If you want the output on terminal then,

如果你想在终端输出那么,

$sed 's/ /,/g' filename.txt

But if you want to edit the file itself i.e. if you want to replace space with the comma in the file then,

但是如果你想编辑文件本身,即如果你想用文件中的逗号替换空格那么,

$sed -i 's/ /,/g' filename.txt

#1


25  

If you are talking about sed, this works:

如果你在谈论sed,这有效:

sed -e "s/ /,/g" < a.txt

In vim, use same regex to replace:

在vim中,使用相同的正则表达式替换:

s/ /,/g

#2


7  

Inside vim, you want to type when in normal (command) mode:

在vim内部,您希望在正常(命令)模式下键入:

:%s/ /,/g

On the terminal prompt, you can use sed to perform this on a file:

在终端提示符下,您可以使用sed在文件上执行此操作:

sed -i 's/\ /,/g' input_file

Note: the -i option to sed means "in-place edit", as in that it will modify the input file.

注意:sed的-i选项意味着“就地编辑”,因为它将修改输入文件。

#3


2  

I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:

我知道这不是你要求的,但是,用换行替换逗号,这很有用:

tr , '\n' < file

#4


1  

Try the following command and it should work out for you.

尝试以下命令,它应该适合你。

sed "s/\s/,/g" orignalFive.csv > editedFinal.csv

#5


1  

If you want the output on terminal then,

如果你想在终端输出那么,

$sed 's/ /,/g' filename.txt

But if you want to edit the file itself i.e. if you want to replace space with the comma in the file then,

但是如果你想编辑文件本身,即如果你想用文件中的逗号替换空格那么,

$sed -i 's/ /,/g' filename.txt