Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Bash:用与单词长度相等的空格替换单词_String_Bash_Parameter Expansion - Fatal编程技术网

String Bash:用与单词长度相等的空格替换单词

String Bash:用与单词长度相等的空格替换单词,string,bash,parameter-expansion,String,Bash,Parameter Expansion,我以为我的拳击足够强壮,但显然不是。我似乎无法理解这一点。我想这样做: var="XXXX This is a line" word_to_replace="XXXX" # ...do something echo "Done:${var}" Done: This is a line 基本上,我想用空格快速替换单词中的所有字符,最好是一步到位。请注意,如果它使事情变得更简单,则var当前将位于字符串的开头,尽管它可能有前导空格(需要保留) 在python中,我可能会

我以为我的拳击足够强壮,但显然不是。我似乎无法理解这一点。我想这样做:

  var="XXXX This is a line"
  word_to_replace="XXXX"
  # ...do something
  echo "Done:${var}"
  Done:     This is a line
基本上,我想用空格快速替换单词中的所有字符,最好是一步到位。请注意,如果它使事情变得更简单,则var当前将位于字符串的开头,尽管它可能有前导空格(需要保留)

在python中,我可能会这样做:

>>> var="XXXX This is a line"
>>> word_to_replace="XXXX"
>>> var=var.replace(word_to_replace, ' '*len(word_to_replace))
>>> print("Done:%s" % var)
Done:     This is a line
我使用GNU Awk:

echo "$title" | gawk '{gsub(/./, "*"); print}'
这将用星号替换每个字符

编辑。综合答案:

$ export text="FOO hello"
$ export sub="FOO"
$ export space=${sub//?/ }
$ echo "${text//$sub/$space}"
    hello
我使用GNU Awk:

echo "$title" | gawk '{gsub(/./, "*"); print}'
这将用星号替换每个字符

编辑。综合答案:

$ export text="FOO hello"
$ export sub="FOO"
$ export space=${sub//?/ }
$ echo "${text//$sub/$space}"
    hello

使用
sed

#!/usr/bin/env sh

word_to_replace="XXXX"
var="$word_to_replace This is a line"

echo "Done: $var"

word_to_replace=$(echo "$word_to_replace" | sed 's,., ,g')
var="$word_to_replace This is a line"
echo "Done: $var"

使用
sed

#!/usr/bin/env sh

word_to_replace="XXXX"
var="$word_to_replace This is a line"

echo "Done: $var"

word_to_replace=$(echo "$word_to_replace" | sed 's,., ,g')
var="$word_to_replace This is a line"
echo "Done: $var"

这里有一种方法,结合使用shell参数扩展和sed命令

$ var="XXXX This is a line"
$ word_to_replace="XXXX"
$ replacement=${word_to_replace//?/ }
$ sed "s/$word_to_replace/$replacement/" <<<"$var"
     This is a line

这里有一种方法,结合使用shell参数扩展和sed命令

$ var="XXXX This is a line"
$ word_to_replace="XXXX"
$ replacement=${word_to_replace//?/ }
$ sed "s/$word_to_replace/$replacement/" <<<"$var"
     This is a line
在普通Bash中:

如果我们知道要替换的单词:

$ line=" foo and some"
$ word=foo
$ spaces=$(printf "%*s" ${#word} "")
$ echo "${line/$word/$spaces}"
     and some
如果我们不这样做,我们可以把字符串分开,找到前导词,但这有点难看:

xxx() {
   shopt -s extglob              # for *( )
   local line=$1
   local indent=${line%%[^ ]*}   # the leading spaces
   line=${line##*( )}            # remove the leading spaces
   local tail=${line#* }         # part after first space 
   local head=${line%% *}        # part before first space...
   echo "$indent${head//?/ } $tail"  # replace and put back together
}
$ xxx "  word on a line"
        on a line
如果行中只有一个单词,
head
tail
都被设置为该单词,那么这也会失败,我们需要检查是否有空格,并分别处理这两个情况。

在普通Bash中:

如果我们知道要替换的单词:

$ line=" foo and some"
$ word=foo
$ spaces=$(printf "%*s" ${#word} "")
$ echo "${line/$word/$spaces}"
     and some
如果我们不这样做,我们可以把字符串分开,找到前导词,但这有点难看:

xxx() {
   shopt -s extglob              # for *( )
   local line=$1
   local indent=${line%%[^ ]*}   # the leading spaces
   line=${line##*( )}            # remove the leading spaces
   local tail=${line#* }         # part after first space 
   local head=${line%% *}        # part before first space...
   echo "$indent${head//?/ } $tail"  # replace and put back together
}
$ xxx "  word on a line"
        on a line


如果行中只有一个单词,
head
tail
都被设置为该单词,那么这也会失败,我们需要检查是否有空格,并分别处理这两个情况。

是否必须在最后一个字符串中填充空格?@RomanPerekhrest是的。我想用与单词长度相等的空格替换这个单词,如标题所述。是否必须在最后一个字符串中填充空格?@RomanPerekhrest是的。我想用与单词长度相等的空格替换这个单词,就像标题提到的那样。这会用星号替换每个字符,这不是所要求的,并且可以通过
tr-c''*'
更容易实现,然后将
tr
或任何内容应用到
$word\u替换
并使用bash字符串替换。顺便说一句,
tr-c''*'
也将替换新行(如果有)。如果从bash选项中删除awk选项和所有
export
s,可能会添加一些解释,那么我认为这将是最好的选项。这将用星号替换每个字符,这不是所要求的,并且可以通过
tr-c''*'
更容易实现,然后将
tr
或任何内容应用到
$word\u替换
并使用bash字符串替换。顺便说一句,
tr-c''*'
也将替换新行(如果有)。如果从bash选项中删除awk选项和所有
export
s,可能会添加一些解释,在我看来,这是最好的选择。这看起来像是一个答案,但并不完全是我想要的,因为它重新创建了一行,而不是替换单词。例如,如我在问题中提到的,如果行在要替换的单词之前有前导空格,该解决方案将如何调整?很好的方法,但是,我可能必须这样做——拆分行,用空格替换单词中的所有字符,然后重新创建行。
newline=${oldline//$word\u to_replace/$replacement}
@Igor ehe…将
$replacement
是什么?@Igor你误解了这个问题。使用您建议的
${word\u to\u replace}
将替换为单个空格。问题是用长度等于
${word\u to\u replace}
的空格替换所有字符。没有。你真的尝试过吗:
${foo/?/*}
。这看起来像是一个答案,但并不完全是我想要的,因为它会重新创建行而不是替换单词。例如,如我在问题中提到的,如果行在要替换的单词之前有前导空格,该解决方案将如何调整?很好的方法,但是,我可能必须这样做——拆分行,用空格替换单词中的所有字符,然后重新创建行。
newline=${oldline//$word\u to_replace/$replacement}
@Igor ehe…将
$replacement
是什么?@Igor你误解了这个问题。使用您建议的
${word\u to\u replace}
将替换为单个空格。问题是用与
${word\u to\u replace}
长度一样多的空格替换所有字符。没有。你真的尝试过吗:
${foo/?/*}
。你不需要
sed
,如果
$word\u to\u replace
包含斜线或一些特殊字符,如点、星号,等。您不需要使用sed,如果
$word\u to\u replace
包含斜杠或一些特殊字符,如点、星号等,这可能会令人惊讶。