bash 如何删除bash中的多余空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13092360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:52:50  来源:igfitidea点击:

How to remove extra spaces in bash?

bash

提问by Charlie

How to remove extra spaces in variable HEAD?

如何删除变量中的多余空格HEAD

HEAD="    how to  remove    extra        spaces                     "

Result:

结果:

how to remove extra spaces

回答by jman

Try this:

尝试这个:

echo "$HEAD" | tr -s " "

or maybe you want to save it in a variable:

或者你想把它保存在一个变量中:

NEWHEAD=$(echo "$HEAD" | tr -s " ")

Update

更新

To remove leading and trailing whitespaces, do this:

要删除前导和尾随空格,请执行以下操作:

NEWHEAD=$(echo "$HEAD" | tr -s " ")
NEWHEAD=${NEWHEAD%% }
NEWHEAD=${NEWHEAD## }

回答by Guru

Using awk:

使用 awk:

$ echo "$HEAD" | awk '='
how to remove extra spaces

回答by glenn Hymanman

Take advantage of the word-splitting effects of not quoting your variable

利用不引用变量的分词效果

$ HEAD="    how to  remove    extra        spaces                     "
$ set -- $HEAD
$ HEAD=$*
$ echo ">>>$HEAD<<<"
>>>how to remove extra spaces<<<

If you don't want to use the positional paramaters, use an array

如果您不想使用位置参数,请使用数组

ary=($HEAD)
HEAD=${ary[@]}
echo "$HEAD"


One dangerous side-effect of not quoting is that filename expansionwill be in play. So turn it off first, and re-enable it after:

不引用的一个危险副作用是文件名扩展将起作用。所以先把它关掉,然后再重新启用它:

$ set -f
$ set -- $HEAD
$ set +f

回答by kojiro

This horse isn't quite dead yet: Let's keep beating it!*

这匹马还没有死:让我们继续击败它!*

Read into array

读入数组

Other peoplehave mentioned read, but since using unquoted expansion may cause undesirable expansions all answers using it can be regarded as more or less the same. You could do

其他人也提到了read,但由于使用不带引号的扩展可能会导致不受欢迎的扩展,因此所有使用它的答案都可以被视为或多或少相同。你可以做

set -f
read HEAD <<< $HEAD
set +f

or you could do

或者你可以做

read -a HEAD <<< "$HEAD"
HEAD="${HEAD[*]}"

Extended Globbing with Parameter Expansion

带参数扩展的扩展 Globbing

$ shopt -s extglob
$ HEAD="${HEAD//+( )/ }" HEAD="${HEAD# }" HEAD="${HEAD% }"
$ printf '"%s"\n' "$HEAD"
"how to remove extra spaces"

*No horses were actually harmed –?this was merely a metaphor for getting six+ diverse answers to a simple question.

*实际上没有马受到伤害——这只是一个比喻,让一个简单的问题得到六个以上不同的答案。

回答by ndbroadbent

Here's how I would do it with sed:

以下是我将如何使用 sed:

string='    how to  remove    extra        spaces                     '
echo "$string" | sed -e 's/  */ /g' -e 's/^ *\(.*\) *$//'

=> how to remove extra spaces   # (no spaces at beginning or end)

The first sed expression replaces any groups of more than 1 space with a single space, and the second expression removes any trailing or leading spaces.

第一个 sed 表达式用一个空格替换任何超过 1 个空格的组,第二个表达式删除任何尾随或前导空格。

回答by Shoo Limberger

Try this one:

试试这个:

echo '    how to  remove    extra        spaces                     ' | sed 's/^ *//g' | sed 's/$ *//g' | sed 's/   */ /g'

or

或者

HEAD="    how to  remove    extra        spaces                     "
HEAD=$(echo "$HEAD" | sed 's/^ *//g' | sed 's/$ *//g' | sed 's/   */ /g')

回答by lisrael1

echo -e " abc \t def "|column -t|tr -s " "

echo -e " abc \t def "|column -t|tr -s " "

column -twill:

column -t将要:

  1. remove the spaces at the beginning and at the end of the line
  2. convert tabs to spaces
  1. 删除行首和行尾的空格
  2. 将制表符转换为空格

tr -s " "will squeeze multiple spaces to single space

tr -s " "将多个空格压缩到单个空格



BTW, to see the whole output you can use cat - -A: shows you all spacial characters including tabs and EOL:

顺便说一句,要查看您可以使用的整个输出cat - -A:向您显示所有空格字符,包括制表符和 EOL:

echo -e " abc \t def "|cat - -A

echo -e " abc \t def "|cat - -A

output: abc ^I def $

输出: abc ^I def $

echo -e " abc \t def "|column -t|tr -s " "|cat - -A

echo -e " abc \t def "|column -t|tr -s " "|cat - -A

output: abc def$

输出: abc def$

回答by Orion

echo variable without quotes does what you want:

不带引号的 echo 变量可以满足您的需求:

HEAD="    how to  remove    extra        spaces      "
echo $HEAD

# or assign to new variable
NEW_HEAD=$(echo $HEAD)
echo $NEW_HEAD

output: how to remove extra spaces

输出: how to remove extra spaces

回答by Frontear

I would make use of trto remove the extra spaces, and xargsto trim the back and front.

我会使用tr删除多余的空格,并使用xargs修剪前后。

TEXT="     This          is  some                             text   "
echo $(echo $TEXT | tr -s " " | xargs)

# [...]$ This is some text