Artificial intelligent assistant

I need to add comma at the end of first 3 words in the text file using shell script I need to add comma at the end of first 3 words in the text file using shell script. ex : file.txt jan Feb Mar Chennai India jan Feb Mar Hyderabad India jan Feb Mar Bangalore India Expected output is : jan,Feb,Mar, Chennai India jan,Feb,Mar, Hyderabad India jan,Feb,Mar, Bangalore India

There are several ways to do this job.

sed can do the work:


$ cat | sed -r 's/(\w+)\s(\w+)\s(\w+)(.*)$/\1, \2, \3,\4/'


read can do the work too:


$ while read c1 c2 c3 rest; do echo $c1, $c2, $c3, $rest; done <


awk can also do the work:


$ cat | awk '{ printf "%s, %s, %s, ", $1, $2, $3; for (i = 4; i <= NF; i++) { printf "%s ", $i; } print ""}'

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 70557712ab1868d84aea97d6d353c0e2