從Bash中的字串中刪除固定的字首/字尾

w36680130發表於2020-05-10

本文翻譯自:Remove a fixed prefix/suffix from a string in Bash

In my bash script I have a string and its prefix/suffix. 在我的bash指令碼中,我有一個字串及其字首/字尾。 I need to remove the prefix/suffix from the original string. 我需要從原始字串中刪除字首/字尾。

For example, let's say I have the following values: 例如,假設我具有以下值:

string="hello-world"
prefix="hell"
suffix="ld"

How do I get to the following result? 如何獲得以下結果?

result="o-wor"

#1樓

參考:https://stackoom.com/question/17kcN/從Bash中的字串中刪除固定的字首-字尾


#2樓

$ foo=${string#"$prefix"}
$ foo=${foo%"$suffix"}
$ echo "${foo}"
o-wor

#3樓

Using sed: 使用sed:

$ echo "$string" | sed -e "s/^$prefix//" -e "s/$suffix$//"
o-wor

Within the sed command, the ^ character matches text beginning with $prefix , and the trailing $ matches text ending with $suffix . 在sed命令中, ^字元匹配以$prefix開頭的文字,結尾的$匹配以$suffix結尾的文字。

Adrian Frühwirth makes some good points in the comments below, but sed for this purpose can be very useful. AdrianFrühwirth在下面的評論中指出了一些要點,但是為此目的使用sed可能非常有用。 The fact that the contents of $prefix and $suffix are interpreted by sed can be either good OR bad- as long as you pay attention, you should be fine. $ prefix和$ suffix的內容由sed解釋的事實可以是好是壞-只要注意,就可以了。 The beauty is, you can do something like this: 美麗之處在於,您可以執行以下操作:

$ prefix='^.*ll'
$ suffix='ld$'
$ echo "$string" | sed -e "s/^$prefix//" -e "s/$suffix$//"
o-wor

which may be what you want, and is both fancier and more powerful than bash variable substitution. 這可能是您想要的,並且比bash變數替換更出色,功能更強大。 If you remember that with great power comes great responsibility (as Spiderman says), you should be fine. 如果您還記得,強大的力量伴隨著巨大的責任(如蜘蛛俠所說),那麼您應該沒事。

A quick introduction to sed can be found at http://evc-cit.info/cit052/sed_tutorial.html 有關sed的快速介紹,請訪問http://evc-cit.info/cit052/sed_tutorial.html。

A note regarding the shell and its use of strings: 關於外殼及其字串使用的說明:

For the particular example given, the following would work as well: 對於給定的特定示例,以下內容同樣適用:

$ echo $string | sed -e s/^$prefix// -e s/$suffix$//

...but only because: ...但僅因為:

  1. echo doesn't care how many strings are in its argument list, and echo不在乎引數列表中有多少個字串,並且
  2. There are no spaces in $prefix and $suffix $ prefix和$ suffix中沒有空格

It's generally good practice to quote a string on the command line because even if it contains spaces it will be presented to the command as a single argument. 通常,在命令列上用引號引起來是一個好習慣,因為即使字串包含空格,它也將作為單個引數顯示給命令。 We quote $prefix and $suffix for the same reason: each edit command to sed will be passed as one string. 出於相同的原因,我們引用$ prefix和$ suffix:sed的每個編輯命令將作為一個字串傳遞。 We use double quotes because they allow for variable interpolation; 我們使用雙引號,因為它們允許變數插值。 had we used single quotes the sed command would have gotten a literal $prefix and $suffix which is certainly not what we wanted. 如果我們使用單引號,則sed命令將獲得原義的$prefix$suffix ,這當然不是我們想要的。

Notice, too, my use of single quotes when setting the variables prefix and suffix . 還要注意,在設定變數prefixsuffix時我使用單引號。 We certainly don't want anything in the strings to be interpreted, so we single quote them so no interpolation takes place. 我們當然不希望字串中的任何內容被解釋,因此我們單引號將它們括起來,因此不會進行插值。 Again, it may not be necessary in this example but it's a very good habit to get into. 同樣,在此示例中可能沒有必要,但這是一個很好的習慣。


#4樓

I use grep for removing prefixes from paths (which aren't handled well by sed ): 我使用grep從路徑中刪除字首( sed處理不好):

echo "$input" | grep -oP "^$prefix\K.*"

\\K removes from the match all the characters before it. \\K從匹配項中刪除所有前面的字元。


#5樓

Do you know the length of your prefix and suffix? 您知道字首和字尾的長度嗎? In your case: 在您的情況下:

result=$(echo $string | cut -c5- | rev | cut -c3- | rev)

Or more general: 或更籠統:

result=$(echo $string | cut -c$((${#prefix}+1))- | rev | cut -c$((${#suffix}+1))- | rev)

But the solution from Adrian Frühwirth is way cool! 但是AdrianFrühwirth解決方案很酷! I didn't know about that! 我不知道!


#6樓

Using @Adrian Frühwirth answer: 使用@AdrianFrühwirth答案:

function strip {
    local STRING=${1#$"$2"}
    echo ${STRING%$"$2"}
}

use it like this 這樣使用

HELLO=":hello:"
HELLO=$(strip "$HELLO" ":")
echo $HELLO # hello

相關文章