如何从字符串中删除空格? [重复]

时间:2021-03-20 22:41:51

This question already has an answer here:

这个问题在这里已有答案:

In ubuntu bash script how to remove space from one variable

在ubuntu bash脚本中如何从一个变量中删除空间

string will be

字符串将是

   3918912k 

Want to remove all blank space.

想要删除所有空白区域。

5 个解决方案

#1


55  

Try doing this in a shell:

尝试在shell中执行此操作:

s="  3918912k"
echo ${s//[[:blank:]]/}

That uses parameter expansion (it's a non feature)

这使用参数扩展(这是一个非posix功能)

[[:blank:]] is a POSIX regex class (remove spaces, tabs...), see http://www.regular-expressions.info/posixbrackets.html

[[:blank:]]是一个POSIX正则表达式类(删除空格,制表符......),请参阅http://www.regular-expressions.info/posixbrackets.html

#2


70  

The tools sed or tr will do this for you by swapping the whitespace for nothing

sed或tr工具可以通过交换空白来为你做这件事

sed 's/ //g'

sed's / // g'

tr -d ' '

tr -d''

Example:

例:

$ echo "   3918912k " | sed 's/ //g'
3918912k

#3


5  

You can also use echo to remove blank spaces, either at the beginning or at the end of the string, but also repeating spaces inside the string.

您还可以使用echo来删除字符串开头或结尾处的空格,还可以在字符串中重复空格。

$ myVar="    kokor    iiij     ook      "
$ echo "$myVar"
    kokor    iiij     ook      
$ myVar=`echo $myVar`
$
$ # myVar is not set to "kokor iiij ook"
$ echo "$myVar"
kokor iiij ook

#4


4  

Since you're using bash, the fastest way would be:

由于你使用bash,最快的方法是:

shopt -s extglob # Allow extended globbing
var=" lakdjsf   lkadsjf "
echo "${var//+([[:space:]])/}"

It's fastest because it uses built-in functions instead of firing up extra processes.

它是最快的,因为它使用内置函数而不是激发额外的进程。

However, if you want to do it in a POSIX-compliant way, use sed:

但是,如果要以符合POSIX的方式执行此操作,请使用sed:

var=" lakdjsf   lkadsjf "
echo "$var" | sed 's/[[:space:]]//g'

#5


2  

A funny way to remove all spaces from a variable is to use printf:

从变量中删除所有空格的一种有趣方法是使用printf:

$ myvar='a cool variable    with   lots of   spaces in it'
$ printf -v myvar '%s' $myvar
$ echo "$myvar"
acoolvariablewithlotsofspacesinit

It turns out it's slightly more efficient than myvar="${myvar// /}", but not safe regarding globs (*) that can appear in the string. So don't use it in production code.

事实证明它比myvar =“$ {myvar // /}”稍微有效,但对于可以出现在字符串中的globs(*)不安全。所以不要在生产代码中使用它。

If you really really want to use this method and are really worried about the globbing thing (and you really should), you can use set -f (which disables globbing altogether):

如果你真的真的想要使用这个方法而且真的很担心全局事物(你真的应该),你可以使用set -f(它完全禁用了globbing):

$ ls
file1  file2
$ myvar='  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it'
$ echo "$myvar"
  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglobfile1file2init
$ # See the trouble? Let's fix it with set -f:
$ set -f
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglob*init
$ # Since we like globbing, we unset the f option:
$ set +f

I posted this answer just because it's funny, not to use it in practice.

我发布这个答案只是因为它很有趣,而不是在实践中使用它。

#1


55  

Try doing this in a shell:

尝试在shell中执行此操作:

s="  3918912k"
echo ${s//[[:blank:]]/}

That uses parameter expansion (it's a non feature)

这使用参数扩展(这是一个非posix功能)

[[:blank:]] is a POSIX regex class (remove spaces, tabs...), see http://www.regular-expressions.info/posixbrackets.html

[[:blank:]]是一个POSIX正则表达式类(删除空格,制表符......),请参阅http://www.regular-expressions.info/posixbrackets.html

#2


70  

The tools sed or tr will do this for you by swapping the whitespace for nothing

sed或tr工具可以通过交换空白来为你做这件事

sed 's/ //g'

sed's / // g'

tr -d ' '

tr -d''

Example:

例:

$ echo "   3918912k " | sed 's/ //g'
3918912k

#3


5  

You can also use echo to remove blank spaces, either at the beginning or at the end of the string, but also repeating spaces inside the string.

您还可以使用echo来删除字符串开头或结尾处的空格,还可以在字符串中重复空格。

$ myVar="    kokor    iiij     ook      "
$ echo "$myVar"
    kokor    iiij     ook      
$ myVar=`echo $myVar`
$
$ # myVar is not set to "kokor iiij ook"
$ echo "$myVar"
kokor iiij ook

#4


4  

Since you're using bash, the fastest way would be:

由于你使用bash,最快的方法是:

shopt -s extglob # Allow extended globbing
var=" lakdjsf   lkadsjf "
echo "${var//+([[:space:]])/}"

It's fastest because it uses built-in functions instead of firing up extra processes.

它是最快的,因为它使用内置函数而不是激发额外的进程。

However, if you want to do it in a POSIX-compliant way, use sed:

但是,如果要以符合POSIX的方式执行此操作,请使用sed:

var=" lakdjsf   lkadsjf "
echo "$var" | sed 's/[[:space:]]//g'

#5


2  

A funny way to remove all spaces from a variable is to use printf:

从变量中删除所有空格的一种有趣方法是使用printf:

$ myvar='a cool variable    with   lots of   spaces in it'
$ printf -v myvar '%s' $myvar
$ echo "$myvar"
acoolvariablewithlotsofspacesinit

It turns out it's slightly more efficient than myvar="${myvar// /}", but not safe regarding globs (*) that can appear in the string. So don't use it in production code.

事实证明它比myvar =“$ {myvar // /}”稍微有效,但对于可以出现在字符串中的globs(*)不安全。所以不要在生产代码中使用它。

If you really really want to use this method and are really worried about the globbing thing (and you really should), you can use set -f (which disables globbing altogether):

如果你真的真的想要使用这个方法而且真的很担心全局事物(你真的应该),你可以使用set -f(它完全禁用了globbing):

$ ls
file1  file2
$ myvar='  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it'
$ echo "$myvar"
  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglobfile1file2init
$ # See the trouble? Let's fix it with set -f:
$ set -f
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglob*init
$ # Since we like globbing, we unset the f option:
$ set +f

I posted this answer just because it's funny, not to use it in practice.

我发布这个答案只是因为它很有趣,而不是在实践中使用它。