Home > Back-end >  Remove only those whitespaces which are appeared after each comma from a string in bash
Remove only those whitespaces which are appeared after each comma from a string in bash

Time:07-20

I am new in bash and would like to know best way to remove those white spaces which are available after each comma in string. For e.g. I have following input string :

abc, xyz,  cdf axy bnz cnm

Resultant string should be :

abc,xyz,cdf axy bnz cnm

CodePudding user response:

You can use sed for this

$ sed -E 's/,  /,/g' input_file
abc,xyz,cdf axy bnz cnm

CodePudding user response:

Turn on the extglob option and do it with parameter expansion pattern replacement:

#!/usr/bin/env bash

str="abc, xyz,  cdf axy bnz cnm"
shopt -s extglob
printf "%s\n" "${str//, ([[:space:]])/,}"
  •  Tags:  
  • bash
  • Related