Announcement

Collapse
No announcement yet.

How to replace multiple consecutive spaces with one comma in Linux?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to replace multiple consecutive spaces with one comma in Linux?

    Can anyone suggest most efficient solution to replace one or more consecutive spaces with one comma?

    I am using Linux.

    Thanks in advance.

  • #2
    Hi Moriana,

    Give this one-liner a try, this will not replace the spaces at the BOL or EOL
    Code:
    awk -v OFS="," '{$1=$1}7' file
    Following line will replace all spaces:
    Code:
    awk '1+gsub(/ +/,",")' file
    Another one with sed, this will replace all spaces, including leading and ending ones
    Code:
    sed 's/ \+/,/g' file

    Comment

    Working...
    X