简体   繁体   English

仅删除 bash 中字符串中每个逗号后出现的那些空格

[英]Remove only those whitespaces which are appeared after each comma from a string in bash

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.我是 bash 的新手,想知道删除字符串中每个逗号后可用的那些空格的最佳方法。 For eg I have following input string :例如,我有以下输入字符串:

abc, xyz,  cdf axy bnz cnm

Resultant string should be :结果字符串应该是:

abc,xyz,cdf axy bnz cnm

You can use sed for this您可以为此使用sed

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

Turn on the extglob option and do it with parameter expansion pattern replacement:打开extglob选项并使用参数扩展模式替换来完成:

#!/usr/bin/env bash

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM