在Unix中用一个逗号替换多个连续的空格 [英] Replace multiple consecutive white spaces with one comma in Unix
问题描述
我有以下sed命令:
sed 's/\s/,/g' input > output.csv
(我从此相关主题)
这将变成以下输入:
SNP A1 A2 FRQ INFO OR SE P
10:33367054 C T 0.9275 0.9434 1.1685 0.1281 0.1843
10:33367707 G A 0.9476 0.9436 1.0292 0.1530 0.8244
10:33367804 G C 0.4193 1.0443 0.9734 0.0988 0.6443
10:33368119 C A 0.9742 0.9343 1.0201 0.1822 0.9156
进入:
SNP,,A1,,A2,,,,,FRQ,,,,INFO,,,,,,OR,,,,,,SE,,,,,,,P
10:33367054,,,C,,,T,,0.9275,,0.9434,,1.1685,,0.1281,,0.1843
10:33367707,,,G,,,A,,0.9476,,0.9436,,1.0292,,0.1530,,0.8244
10:33367804,,,G,,,C,,0.4193,,1.0443,,0.9734,,0.0988,,0.6443
10:33368119,,,C,,,A,,0.9742,,0.9343,,1.0201,,0.1822,,0.9156
我需要一个将多个连续空格变成一个逗号的命令,以提供如下输出:
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
有什么想法吗?
如果要使用sed
,则可以使用以下代码:
$ sed 's/ \{1,\}/,/g' file
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
它基于格伦·杰克曼(glenn jackman)对如何使用sed剥离多位空格的答案?.. >
也可以像
sed 's/[[:space:]]\{1,\}/,/g' file
请注意,您可以使用sed -i.bak '...' file
进行就地编辑,以便将原始文件备份为file.bak
,并且file
将具有编辑的内容.
但是我认为使用tr
会更清楚.使用它,您可以压缩空格,然后用逗号替换每个空格:
$ tr -s ' ' < file | tr ' ' ','
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
按件:
$ tr -s ' ' < file
SNP A1 A2 FRQ INFO OR SE P
10:33367054 C T 0.9275 0.9434 1.1685 0.1281 0.1843
10:33367707 G A 0.9476 0.9436 1.0292 0.1530 0.8244
10:33367804 G C 0.4193 1.0443 0.9734 0.0988 0.6443
10:33368119 C A 0.9742 0.9343 1.0201 0.1822 0.9156
来自man tr
:
tr [OPTION] ... SET1 [SET2]
翻译,压缩和/或删除标准输入中的字符, 写入标准输出.
-s ,--squeeze-repeats
替换列出的重复字符的每个输入序列 一次出现该字符的SET1
I have the following sed command:
sed 's/\s/,/g' input > output.csv
(I got the command from this related topic)
which turns the following input:
SNP A1 A2 FRQ INFO OR SE P
10:33367054 C T 0.9275 0.9434 1.1685 0.1281 0.1843
10:33367707 G A 0.9476 0.9436 1.0292 0.1530 0.8244
10:33367804 G C 0.4193 1.0443 0.9734 0.0988 0.6443
10:33368119 C A 0.9742 0.9343 1.0201 0.1822 0.9156
into:
SNP,,A1,,A2,,,,,FRQ,,,,INFO,,,,,,OR,,,,,,SE,,,,,,,P
10:33367054,,,C,,,T,,0.9275,,0.9434,,1.1685,,0.1281,,0.1843
10:33367707,,,G,,,A,,0.9476,,0.9436,,1.0292,,0.1530,,0.8244
10:33367804,,,G,,,C,,0.4193,,1.0443,,0.9734,,0.0988,,0.6443
10:33368119,,,C,,,A,,0.9742,,0.9343,,1.0201,,0.1822,,0.9156
I need a command that turns the multiple consecutive spaces into just one commma, to give me an output like this:
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
Any ideas?
If you want to use sed
, you can use this one:
$ sed 's/ \{1,\}/,/g' file
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
It is based on glenn jackman's answer to How to strip multipe spaces to one using sed?.
It can also be like
sed 's/[[:space:]]\{1,\}/,/g' file
And note you can use sed -i.bak '...' file
to get an in place edit, so that the original file will be backed up as file.bak
and file
will have the edited content.
But I think it is more clear with tr
. With it, you can squeeze the spaces and then replace each one of them with a comma:
$ tr -s ' ' < file | tr ' ' ','
SNP,A1,A2,FRQ,INFO,OR,SE,P
10:33367054,C,T,0.9275,0.9434,1.1685,0.1281,0.1843
10:33367707,G,A,0.9476,0.9436,1.0292,0.1530,0.8244
10:33367804,G,C,0.4193,1.0443,0.9734,0.0988,0.6443
10:33368119,C,A,0.9742,0.9343,1.0201,0.1822,0.9156
By pieces:
$ tr -s ' ' < file
SNP A1 A2 FRQ INFO OR SE P
10:33367054 C T 0.9275 0.9434 1.1685 0.1281 0.1843
10:33367707 G A 0.9476 0.9436 1.0292 0.1530 0.8244
10:33367804 G C 0.4193 1.0443 0.9734 0.0988 0.6443
10:33368119 C A 0.9742 0.9343 1.0201 0.1822 0.9156
From man tr
:
tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
-s, --squeeze-repeats
replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
这篇关于在Unix中用一个逗号替换多个连续的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!