Linux 带有正则表达式的 Bash 子字符串

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

Bash substring with regular expression

linuxbashsubstring

提问by Luis Andrés García

In a bash script, I′d like to extract a variablestring from a given string. I mean, i′d like to extract the string file.txtfrom the string:

在 bash 脚本中,我想从给定的字符串中提取一个变量字符串。我的意思是,我想file.txt从字符串中提取字符串:

This is the file.txt from my folder.

I tried:

我试过:

var=$(echo "This is the file.txt from my folder.")
var=echo ${var##'This'}
...

but I′d like to make it in a cleaner way, using the expr, sedor awkcommands.

但我想以更简洁的方式使用expr,sedawk命令。

Thanks

谢谢

Edited:

编辑:

I found another way (nevertheless, the answer with the sed command is the best one for me):

我找到了另一种方法(尽管如此, sed 命令的答案对我来说是最好的):

var=$(echo 'This is the file.txt from my folder.')
front=$(echo 'This is the ')
back=$(echo ' from my folder.')
var=${var##$front}
var=${var%$back} 
echo $var

采纳答案by Daniel S.

The following solution uses sedwith s/(substitution) to remove the leading and trailing parts:

以下解决方案使用sedwith s/(substitution) 删除前导和尾随部分:

echo "This is the file.txt from my folder." | sed "s/^This is the \(.*\) from my folder.$//"

Output:

输出:

file.txt

The \(and \)enclose the part which we want to keep. This is called a group. Because it's the first (and only) group which we use in this expression, it's group 1. We later reference this group inside of the replacement string with \1.

\(\)包围,我们要保留的部分。这称为组。因为它是我们在这个表达式中使用的第一个(也是唯一一个)组,所以它是组 1。我们稍后在替换字符串中使用\1.

The ^and $signs make sure that the complete string is matched. This is only necessary for the special case that the filename contains either "from my folder."or "This is the".

^$体征确保整个字符串匹配。这仅适用于文件名包含"from my folder."或的特殊情况"This is the"

回答by EverythingRightPlace

You could try grep:

你可以试试grep:

var=$(egrep -o file.txt)

回答by Dániel

If 'file.txt' is a fixed string, and won't change, then you can do it like this:

如果 'file.txt' 是一个固定字符串,并且不会改变,那么你可以这样做:

var="This is the file.txt from my folder"

var="This is the file.txt from my folder"

Notice that you don't need to echo the string to the variable, you just type it on the right side of the binary '=' operator.

请注意,您不需要将字符串回显到变量中,只需在二进制“=”运算符的右侧键入它即可。

echo $var |sed -e 's/^.*\(file\.txt\).*$/\1/'

echo $var |sed -e 's/^.*\(file\.txt\).*$/\1/'

Depending on your sed(1) version, you can loose the escaping of the parenthesis if you have the -r (extended regexp) option in sed(1).

根据您的 sed(1) 版本,如果您在 sed(1) 中有 -r(扩展正则表达式)选项,则可以取消括号的转义。

If 'file.txt' changes, than you can create a pattern on a best effort basis, like:

如果 'file.txt' 更改,那么您可以尽最大努力创建一个模式,例如:

echo $var |sed -e 's/^.* \([^ ]\+\.[^ ]\+\) .*$/\1/'

echo $var |sed -e 's/^.* \([^ ]\+\.[^ ]\+\) .*$/\1/'