通过BASH用下划线替换空格

时间:2022-01-02 18:31:22

Suppose i have a string, $str. I want $str to be edited such that all the spaces in it are replaced by underscores.

假设我有一个字符串$ str。我想要对$ str进行编辑,使其中的所有空格都被下划线替换。

Example

a="hello world"

I want the final output of

我想要最终的输出

echo "$a"

to be hello_world

是hello_world

4 个解决方案

#1


17  

You could try the following:

您可以尝试以下方法:

str="${str// /_}"

#2


9  

Pure bash:

纯粹的bash:

a="hello world"
echo "${a// /_}"

OR tr:

或者tr:

tr -s ' ' '_' <<< "$a"

#3


5  

$ a="hello world"
$ echo ${a// /_}
hello_world

According to bash(1):

根据bash(1):

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced
with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

模式替换。扩展模式以生成模式,就像路径名扩展一样。扩展参数,并将模式与其值的最长匹配替换为字符串。如果pattern以/开头,则pattern的所有匹配都将替换为string。通常只替换第一场比赛。如果pattern以#开头,则它必须在参数的扩展值的开头匹配。如果pattern以%开头,则它必须在参数的扩展值的末尾匹配。如果string为null,则删除pattern的匹配,并且可以省略/ following模式。如果参数是@或*,则替换操作依次应用于每个位置参数,并且扩展是结果列表。如果parameter是使用@或*下标的数组变量,则替换操作依次应用于数组的每个成员,并且扩展是结果列表。

#4


3  

With sed reading directly from a variable:

sed直接从变量读取:

$ sed 's/ /_/g' <<< "$a"
hello_world

And to store the result you have to use the var=$(command) syntax:

要存储结果,您必须使用var = $(命令)语法:

a=$(sed 's/ /_/g' <<< "$a")

For completeness, with awk it can be done like this:

为了完整性,使用awk可以这样做:

$ a="hello my name is"
$ awk 'BEGIN{OFS="_"} {for (i=1; i<NF; i++) printf "%s%s",$i,OFS; printf "%s\n", $NF}' <<< "$a"
hello_my_name_is

#1


17  

You could try the following:

您可以尝试以下方法:

str="${str// /_}"

#2


9  

Pure bash:

纯粹的bash:

a="hello world"
echo "${a// /_}"

OR tr:

或者tr:

tr -s ' ' '_' <<< "$a"

#3


5  

$ a="hello world"
$ echo ${a// /_}
hello_world

According to bash(1):

根据bash(1):

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced
with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

模式替换。扩展模式以生成模式,就像路径名扩展一样。扩展参数,并将模式与其值的最长匹配替换为字符串。如果pattern以/开头,则pattern的所有匹配都将替换为string。通常只替换第一场比赛。如果pattern以#开头,则它必须在参数的扩展值的开头匹配。如果pattern以%开头,则它必须在参数的扩展值的末尾匹配。如果string为null,则删除pattern的匹配,并且可以省略/ following模式。如果参数是@或*,则替换操作依次应用于每个位置参数,并且扩展是结果列表。如果parameter是使用@或*下标的数组变量,则替换操作依次应用于数组的每个成员,并且扩展是结果列表。

#4


3  

With sed reading directly from a variable:

sed直接从变量读取:

$ sed 's/ /_/g' <<< "$a"
hello_world

And to store the result you have to use the var=$(command) syntax:

要存储结果,您必须使用var = $(命令)语法:

a=$(sed 's/ /_/g' <<< "$a")

For completeness, with awk it can be done like this:

为了完整性,使用awk可以这样做:

$ a="hello my name is"
$ awk 'BEGIN{OFS="_"} {for (i=1; i<NF; i++) printf "%s%s",$i,OFS; printf "%s\n", $NF}' <<< "$a"
hello_my_name_is