Replace multiple consecutive spaces with a single comma in Unix

I have a sed command:

sed 's/\s/,/g' input > output.csv

(I got a command for this topic)

which includes 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

at

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 several consecutive spaces into one commie to give me this conclusion:

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?

+6
source share
3 answers

If you want to use sed, you can use this:

$ 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 to remove space between several ways using sed? .

It can also be like

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 []... SET1 [SET2]

, / , .

-s, --squeeze-repeatts

, SET1

+10

-r, + \s, :

$ sed -r 's/\s+/,/g' file.txt
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

:

-r, --regexp-extended
    use extended regular expressions in the script.

. Mac OS X, sed BSD GNU, -E:

-E    Interpret regular expressions as extended (modern) regular expressions rather
      than basic regular expressions (BRE's). The re_format(7) manual page fully 
      describes both formats.
+8

Here is a very simple solution with awk

awk '{$1=$1}1' OFS=, 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

$1=$1 reformat the file so that all extra spaces are set to the same space.

+6
source

Source: https://habr.com/ru/post/1536755/


All Articles