How to format file into comma separated field

Guys,

Need you help, i have a a file content that look like this.

Nokia 3330 <spaces><spaces><more spaces>+76451883874
Nokia 3610 +87467361615
so on and so forth,

  • there are so many spaces in between.
  • e.g. there should be space between Nokia and 3330(model)

i like the output that look like this.
Nokia 3330,+76451883874
Nokia 3610,+87467361615

Try,

$ cat inputfile | tr -s ' ' | nawk '{ sub(/ \+/,",+"); print $0 }' 

try

tr -s " " " " <filename

or

GNU sed

sed -r 's/[ ]{2}/ /g' filename

Non-GNU sed

sed 's/[ ]\{2,\}/g' filename

Please see this is only to remove 2 or more consecutive spaces NOT to add comma's

Regards,

1 Like

Hi agn,

Thanks it works!

Can you share how does this part works ?
nawk '{ sub(/ \+/,",+"); print $0 }'

Br
Sherwin

tr -s ' '

This squeezes spaces.

nawk '{ sub(/ \+/,",+"); print $0 }

This substitues a space followed by + with a , and + and then prints the whole line ($0).

Hi agn,

thanks again. :slight_smile: