Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the table.:(:frowning:

tried this command

tr ' ' ',' < input_file.txt > output_file.txt

but it is converting eachspace into a single charecter so the output is like

name,,,,,age,,,,job,,,,,, 

where each space is replaced by comma.

i just want all the spaces in between each column into a single comma like the below one

name,age,job

Please help me with a command or a set of commands to convert the output in the format i asked.

Welcome to the forum.

The first place to look at is the data creator, i.e. your SQL query. Can it be modified to supply exactly what you need without correction / adaption downstream? My SQL has become a bit rusty, but there exist settings to influence the output like set colsep , or functions to remove spaces e.g. trim() . Does this help?

Re. your request: does your tr version offer the -s (squeeze) option?

tr -s ' ' ',' < file
name,age,job,

If not, we need to dive deeper and resort to a small script like for awk or similar.

No trailing " , "s ?

awk '$1=$1' OFS=, input_file.txt
sed 's/  */,/g; s/,*$//' input_file.txt