Replace multiple consecutive white spaces with one comma in Unix
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
Remove all white space in a file and replace them with a comma using Vim
First delete the blank lines:
:g/^\s*$/d
Then use a substitution (:s///
) over each line (%
) to replace all (g
) continuous whitespace (\s\+
) with a comma (,
).
:%s/\s\+/,/g
Replace spaces with commas except for text inside brackets
Using GNU awk for FPAT
:
$ awk -v FPAT='[^ ]*|[[][^]]+]' -v OFS=',' '{$1=$1}1' file
[Royal Gauntlets of Silvermoon],(1),Senhna,2500g
[Chestguard of the Vanquished Hero],(1),Neithia,3000g
[Chestguard of the Vanquished Hero],(1),Buddafly,3000g
How to replace space with comma using sed?
If you are talking about sed
, this works:
sed -e "s/ /,/g" < a.txt
In vim
, use same regex to replace:
s/ /,/g
How do I replace first space with comma in UNIX
You can use this sed
command
sed -r 's/\s+/,/' File_Name
or
sed -r 's/ +/,/' File_Name
-r, --regexp-extended
use extended regular expressions in the script.
Output :
A,B
123,Hi There
234,Hello there
how to replace every 6th ooccurrence blank space with new line from a file?
This is actually a pretty tricky thing to do. Here are a few ways:
sed: convert all whitespace to commas, then replace every 6th comma with a newline.
sed -r 's/[[:blank:]]+/,/g; s/([^,]+(,[^,]+){5}),/\1\n/g' file
awk, print each field and decide what separator to use for each one.
awk '{
for (i=1; i<=NF; i++)
printf "%s%s", $i, (i == NF ? "" : ( i%6 == 0 ? "\n" : ","))
print ""
}' filebash
myjoin() { local IFS=$1; shift; echo "$*"; }
while read -ra words; do
while (( ${#words[@]} > 0 )); do
myjoin , "${words[@]:0:6}"
words=( "${words[@]:6}" )
done
done < filethis one's my favourite:
tr
to convert whitespace to newlines;paste
to print 6 fields per line;sed
to clean up trailing commas on the last linetr -s '[:blank:]' '\n' < file | paste -d, - - - - - - | sed '$s/,\+$//'
This one acts differently from the others though: if your input file has 3 lines of 8 words, all the other methods will output 6 lines, odd-numered lines with 6 fields and even-numbered lines with 2 fields. This answer will print 4 lines, all having 6 fields. So, depends on your need.
Replace first two whitespace occurrences with a comma using sed
How about this:
sed -e 's/\s\+/,/' | sed -e 's/\s\+/,/'
It's probably possible with a single sed command, but this is sure an easy way :)
My output:
a,b,1 2 3 3 2 1
c,d,44 55 66 2355
line,http://google.com,100 200 300
ef,jh,77 88 99
z,y,2 3 33
Replacing all commas with spaces - S/R with a Unix Command?
sed 's/,/ /g' filename >resultfile
Related Topics
Negate If Condition in Bash Script
How to Check If Sed Has Changed a File
How to Change Bash History Completion to Complete What's Already on the Line
How to Monitor a Complete Directory Tree for Changes in Linux
Joining Multiple Fields in Text Files on Unix
Insert Multiple Lines into a File After Specified Pattern Using Shell Script
Relinking an Anonymous (Unlinked But Open) File
How to Increase Ble Advertisement Frequency in Bluez
How to Set Rpath and Runpath with Gcc/Ld
Ioctl VS Netlink VS Memmap to Communicate Between Kernel Space and User Space
Differencebetween Double-Ampersand (&&) and Semicolon (;) in Linux Bash
Linux: Remove File Extensions for Multiple Files
How to Pass a Wildcard Parameter to a Bash File
Is Garbage Allowed in High Bits of Parameter and Return Value Registers in X86-64 Sysv Abi
Application Control of Tcp Retransmission on Linux
Maximum Number of Concurrent Connections on a Single Port (Socket) of Server