Script using awk to replace space by comma

I have the content of this file that i would like to replace the space by comma.
The file content values in this format

FName LName     Date & time
------------------------------------
Gilles    John    14/12/17  12:30:45

I want this format

Fname,LName,Date&time
-----------------------------
Gilles,John,14/12/17 12:30:45

I used the below, but i'm getting the comma between date and time

awk '{$1=$1;print}'  SIM_Sale.csv | sed 's! |!|!g' | sed 's!| !|!g' |tr ' ' ','

That is an extremely complicated pipeline that doesn't seem to be accomplishing a lot of what you want to do. The output you say you want also removes some spaces and hyphens that don't fit with your stated desire to change spaces to commas when used as field separators between the first three fields.

The following trims the 2nd line of the input to the length of the modified 1st line. (Assuming that there will typically be more than three lines of input in the files you process, you can't adjust the 2nd line of the file to match the longest output line that will be produced unless you store the entire output in memory before printing it, or reading the input file twice.)

awk '
NR == 2 {
	printf("%*.*s\n", w - 1, w - 1, $0)
	next
}
{	for(i = 1; i <= NF; i++) {
		printf("%s%s", $i, (i < 3) ? "," : (i == NF) ? ORS : OFS)
		w += length($i) + 1
	}
}' SIM_Sale.csv

With your sample input, this produces the output:

FName,LName,Date & time
-----------------------
Gilles,John,14/12/17 12:30:45

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

P.S. I had a cut-and-paste error omitting the 1st line of the awk script (as noted by Scrutinizer in post #3). That missing line has now been added above.

How about:

sed 's/  */,/; s/  */,/' file

or a different approach:

awk '!/---/{$3=$3 FS $4 FS $5; $4=$5=x; sub(/,+$/,x)}1' OFS=, file

---
I think Don Cragun left out the following:

1 Like
tr -s ' ' ','

should do the trick, surely.

Thanks it has helped a lot with 3 fields. But if i have more fields like below

agent_msisdn    agent_username  customer_msisdn        datetime            customer_firstname      customer_othernames     customer_surname     customer_gender customer_address_line1  customer_address_line2  customer_address_line3  customer_address_city
233246250391          NULL         0246250391       2017-12-12 11:19:16     Anicet Williams                0                   OWUSU                  1                 0                     0                     Centrale HO              2

which parameter to change?

---------- Post updated at 08:31 AM ---------- Previous update was at 08:30 AM ----------

Thanks it has helped a lot with 3 fields. But if i have more fields like below

agent_msisdn    agent_username  customer_msisdn        datetime            customer_firstname      customer_othernames     customer_surname     customer_gender customer_address_line1  customer_address_line2  customer_address_line3  customer_address_city
233246250391          NULL         0246250391       2017-12-12 11:19:16     Anicet Williams                0                   OWUSU                  1                 0                     0                     Centrale HO              2

which parameter to change?

sed ' s/ *& /\&/g; s/\([0-9]\)  *\([0-9][0-9]\):/\1:!:\2:/g; s/[ \t][ \t]*/,/g; s/:!:/ /g; ' infile

@rdrtx1 it's exactly what i needed. Thank you very much.

agent_msisdn,agent_username,customer_msisdn,datetime,customer_firstname,customer_othernames,customer_surname,customer_gender,customer_address_line1,customer_address_line2,customer_address_line3,customer_address_city
233246250391,NULL,0246250391,2017-12-12 11:19:16,Anicet,Williams,0,OWUSU,1,0,0,Centrale,Okoyo

Sure you want Anicet Williams split into two fields? As long as field separators (here: spaces) can occur within field values, there's no reliable way to tell one from the other.