Bash 脚本,如何删除尾随子字符串,不区分大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4329886/
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-17 23:01:25  来源:igfitidea点击:

Bash scripting, how to remove trailing substring, case insensitive?

bashstringcase-insensitive

提问by basher1

I am modifying a script that reads in a user email. It is very simple, too simple.

我正在修改一个读取用户电子邮件的脚本。这很简单,太简单了。

echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"

This works, but only for lower case @example.com. How could I modify this to remove the trailing @example.com, case insensitive?

这有效,但仅适用于小写@example.com。我如何修改它以删除尾随的@example.com,不区分大小写?

采纳答案by kanaka

If you have bash 4:

如果你有 bash 4:

email=${email,,}
email=${email%%@example.com}

Otherwise, perhaps just use tr:

否则,也许只需使用 tr:

email=$(echo "${email}" | tr "A-Z" "a-z")
email=${email%%@example.com}

Update:

更新

If you are just wanting to strip the host (any host) then perhaps this is really what you want:

如果您只是想剥离主机(任何主机),那么这可能就是您真正想要的:

email=${email%%@*}

回答by Paused until further notice.

For Bash 3.2 and greater:

对于 Bash 3.2 及更高版本:

shopt -s nocasematch
email='[email protected]'
pattern='^(.*)@example.com$'
[[ $email =~ $pattern ]]
email=${BASH_REMATCH[1]}    # result: JoHnDoE

回答by thkala

How about using sed?

使用sed怎么样?

email="$(sed 's|@example\.com$||i' <<<"$email")"

Note the 'i' flag in the sed substitution command which requests case-insensitive matching.

请注意 sed 替换命令中的“i”标志,该命令要求不区分大小写的匹配。

回答by carlo

Here's yet another (though a bit lengthy) take on it:

这是另一个(虽然有点冗长)的看法:

email='[email protected]'
email=${email%%@[Ee][Xx][Aa][Mm][Pp][Ll][Ee].[Cc][Oo][Mm]}
echo "email is $email"

回答by feklee

Use case insensitive matching to a regular expression:

使用不区分大小写的正则表达式匹配:

echo -n "Please enter your example.com email address: "
read email
shopt -s nocasematch # Makes comparisons case insensitive
if [[ $email =~ example.com$ ]]; then
    domain=${BASH_REMATCH[0]}
    echo "email is ${email%%@$domain}"
fi

Alternatively, you can cut off a string based on its length (which only makes sense if you know that the user entered the domain correctly):

或者,您可以根据字符串的长度截断字符串(这仅在您知道用户正确输入域时才有意义):

echo -n "Please enter your example.com email address: "
read email
domain=example.com
email_length=${#email}
domain_length=${#domain}
name_length=$(($email_length-$domain_length-1))
email=${email:0:$name_length} # removes trailing @example.com from email
echo "email is $email"

回答by Hsin

echo -n "Please enter your example.com email address: "
read _email
_email=$(echo $_email | awk 'BEGIN{ FS="@" }  {print }')
echo $_email

and for transfer email to lower case you can use declare, such as

并将电子邮件传输到小写,您可以使用声明,例如

declare -l email=$email

声明 -l email=$email