Substitute space with comma in shell scripting

sed replace multiple spaces with comma
bash replace newline with comma
python replace space with comma in a file
sed replace colon with space
sed replace with blank space
sed replace space with %20
unix replace space with pipe
linux convert space delimited to csv

I'm trying to substitute blank spaces with a comma the output of ls -l. (To make a long story short I want to separate columns with a comma)

I have tried this

ls -l *.nc | sed -e 's/ /,/g' 

but I get a comma in every blank space between a column and another. Instead I wish to get a comma where there's "at least" 1 blank space. So if there are 10 blank spaces between a column and the next one, I want to insert only 1 comma.

Any suggestions?

You have to squeeze spaces: convert multiple spaces into just one. This can be done with tr -s ' ':

ls -l *.nc | tr -s ' ' | sed -e 's/ /,/g' 

From man tr:

-s, --squeeze-repeats

replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character

You can also tell sed to do all the job:

ls -l *.nc | sed 's/[ \t]+*/,/g'
                    ^^^^^^^ ^
                      |     replace with comma
                    get one or multiple spaces and tabs

Remove blank spaces with comma in a string in bash shell, 9 Answers. Substitutes each space with a comma, if you need you can make a pass with the -s flag (squeeze repeats), that replaces each input sequence of a repeated character that is listed in SET1 (the blank space) with a single occurrence of that character. See “Shell Scripting: Convert Uppercase to Lowercase” for more info. Summary: String Manipulation and Expanding Variables. For your ready references here are all your handy bash parameter substitution operators. Try them all; enhance your scripting skills like a pro:

Replace whitespace with a comma in a text file in Linux, Which command will translate all the white space to tabs? Using the shell substitution command, all the commas are replaced with the colons. '${line/,/:}' will replace only the 1st match. The extra slash in '${line/ /,/:}' will replace all the matches. Note: This method will work in bash and ksh93 or higher, not in all flavors. 8. Shell script using IFS to change the delimiter of file:

If for some reason these methods do not help, you can try with paste

ls -l *.nc | paste -s -d ","

where -d "," is a delimiter

Replace whitespaces with tabs in linux, How do you replace a tab with a comma in Unix? The problem with this approach is that the array element are divided on ‘space delimiter’. Because of that, elements like ‘Linux Mint’ will be treated as two words. Here’s the output of the above script: Ubuntu Linux Mint Debian Arch Fedora. That’s the reason why I prefer the first method to split string in bash.

How can I convert tab delimited data to comma delimited data , Hi, what is the better way to replace the , (comma) with a space char? Example: pre { overflow:scroll; margin:2px; padding:15px; border:3px inset;  Here I replace a comma with a period, but you can choose any symbol you want in the Join parameter: $Var = Get-ADuser -Identity Test,User $Var.Name.Split(',') -Join '.' This should produce the desired outcome, but you will most likely need to use it in a ForEach ($V in $Var) if you have multiple objects needing to be formatted

Replace , (comma) with space, I'm fairly new to UNIX and I need some help on the commands sed and awk. Here is an example: val1 val2 val3 val4 1 2 3 4 11 22 33 44 I need to remove the​  append the current and next line to the pattern space via N if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)). finally the substitution replaces every newline with a comma on the pattern space (which is the whole file).

SED command to replace whitespace with comma, I have simple script that takes 4 variables from interactive keyboard 'read' however my command has a comma in it. I read on IFS separator value,  Buy Shell Scripts at Amazon. Free Shipping on Qualified Orders.

  • I've sorted out with "tr" --> tr -s '[:blank:]' ','
  • Your request is a bit confusing. In the title you say "substitute", in the question you say "insert only 1 comma". Please provide an example of the output you want.
  • Great that you introduced a few different ways to do it, I wasn't aware of the tr command