I need to replace all white spaces inside my text with commas. I'm currently using this line but it doesn't work: I get as output a text file which is exactly the same of the original one:
sed 's/[:blank:]+/,/g' orig.txt > modified.txt
thanks
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI need to replace all white spaces inside my text with commas. I'm currently using this line but it doesn't work: I get as output a text file which is exactly the same of the original one:
sed 's/[:blank:]+/,/g' orig.txt > modified.txt
thanks
With GNU sed
:
sed -e 's/\s\+/,/g' orig.txt > modified.txt
Or with perl
:
perl -pne 's/\s+/,/g' < orig.txt > modified.txt
Edit: To exclude newlines in Perl you could use a double negative 's/[^\S\n]+/,/g'
or match against just the white space characters of your choice 's/[ \t\r\f]+/,/g'
.
+
operator in sed, I just updated my answer. Newlines are whitepsace in perl, so you will have to do your whitepsace class manually if you don't wnat to include it. I added two solutions for that as wel.
Using tr
:
tr -s '[:blank:]' ',' <file
This will replace any horizontal whitespace with a comma. Any repeated whitespace will only be replaced with a single comma.
The issue with your command,
sed 's/[:blank:]+/,/g' orig.txt > modified.txt
is twofold:
The [:blank:]
is a bracketed expression matching one of the characters :
, a
, b
, k
, l
, or n
. If you want to match one of the characters in the POSIX character class [:blank:]
, use [[:blank:]]
.
The +
is an extended regular expression modifier. The sed
utility uses basic regular expressions, and the +
would match a literal +
character. To get the same effect in a basic regular expression, use \{1,\}
, or, in this particular expression, use [[:blank:]][[:blank:]]*
instead.
In short, your corrected sed
expression would be
sed 's/[[:blank:]]\{1,\}/,/g' orig.txt >modified.txt
or
sed 's/[[:blank:]][[:blank:]]*/,/g' orig.txt >modified.txt
As others have pointed out, and assuming single byte ASCII characters in the text, it may be more efficient to use tr
in this case, as it's a simple transliteration of one set of characters to another. Either one of the following two tr
commands would solve the issue:
tr -s '[:blank:]' '[,*]' <orig.txt >modified.txt
where [,*]
means "as many commas as is needed for this set to match the number of characters in the first set", or
tr -s '\t ' ',,' <orig.txt >modified.txt
The -s
option to tr
causes multiple consecutive commas to be "squeezed" into single commas.
tr
implementations including GNU tr
only work correctly with single-byte characters, so they would fail to handle blank characters such as U+1680 OGHAM SPACE MARK, U+2000 EN QUAD, U+2001 EM QUAD, U+2002 EN SPACE, U+2003 EM SPACE, U+2004 THREE-PER-EM SPACE, U+2005 FOUR-PER-EM SPACE, U+2006 SIX-PER-EM SPACE, U+2008 PUNCTUATION SPACE, U+2009 THIN SPACE, U+200A HAIR SPACE, U+205F MEDIUM MATHEMATICAL SPACE, U+3000 IDEOGRAPHIC SPACE in those locales where those characters are encoded on more than one byte (like those using UTF-8 as their charmap)...
Mar 6, 2021 at 11:45
~$ vim filename
:set nu
(just for setting lines):%s/ /,/g
(this will replace space to comma):wq!
(save and exit)awk '{print $1","}' Servers | tr -d '[\n]' | xargs echo
Here the file name is Servers.. It contains the below content..
Server1
Server2
Server3
If we use the above command we can get the below output..
Server1,Server2,Server3,
[mohramak@oc3246038448 Desktop]$ cat Servers
Server1
Server2
Server3
[mohramak@oc3246038448 Desktop]$ awk '{print $1","}' Servers | tr -d '[\n]' | xargs echo
Server1,Server2,Server3,
[mohramak@oc3246038448 Desktop]$