Linux 在 Bash 脚本中返回正则表达式匹配,而不是替换它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1898553/
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
Return a regex match in a Bash script, instead of replacing it
提问by Mint
I just want to match some text in a Bash script. I've tried using sed but I can't seem to make it just output the match instead of replacing it with something.
我只想匹配 Bash 脚本中的一些文本。我试过使用 sed 但我似乎无法让它只输出匹配而不是用某些东西替换它。
echo -E "TestT100String" | sed 's/[0-9]+/dontReplace/g'
Which will output TestTdontReplaceString
.
哪个将输出TestTdontReplaceString
.
Which isn't what I want, I want it to output 100
.
这不是我想要的,我希望它输出100
.
Ideally, it would put all the matches in an array.
理想情况下,它将所有匹配项放在一个数组中。
edit:
编辑:
Text input is coming in as a string:
文本输入以字符串形式输入:
newName()
{
#Get input from function
newNameTXT=""
if [[ $newNameTXT ]]; then
#Use code that im working on now, using the $newNameTXT string.
fi
}
采纳答案by Paused until further notice.
echo "TestT100String" | sed 's/[^0-9]*\([0-9]\+\).*//'
echo "TestT100String" | grep -o '[0-9]\+'
The method you use to put the results in an array depends somewhat on how the actual data is being retrieved. There's not enough information in your question to be able to guide you well. However, here is one method:
用于将结果放入数组的方法在某种程度上取决于检索实际数据的方式。您的问题中没有足够的信息能够很好地指导您。但是,这是一种方法:
index=0
while read -r line
do
array[index++]=$(echo "$line" | grep -o '[0-9]\+')
done < filename
Here's another way:
这是另一种方式:
array=($(grep -o '[0-9]\+' filename))
回答by Mic
Use grep. Sed is an editor. If you only want to match a regexp, grep is more than sufficient.
使用 grep。Sed 是一个编辑器。如果您只想匹配正则表达式,grep 就足够了。
回答by John Kugelman
You could do this purely in bash using the double square bracket [[ ]]
test operator, which stores results in an array called BASH_REMATCH
:
您可以使用方括号[[ ]]
测试运算符纯粹在 bash 中执行此操作,该运算符将结果存储在名为 的数组中BASH_REMATCH
:
[[ "TestT100String" =~ ([0-9]+) ]] && echo "${BASH_REMATCH[1]}"
回答by tomkaith13
Well , the Sed with the s/"pattern1"/"pattern2"/g just replaces globally all the pattern1s to pattern 2.
好吧,带有 s/"pattern1"/"pattern2"/g 的 Sed 只是将所有的 pattern1 全局替换为模式 2。
Besides that, sed while by default print the entire line by default . I suggest piping the instruction to a cut command and trying to extract the numbers u want :
除此之外, sed while 默认情况下默认打印整行。我建议将指令传递给 cut 命令并尝试提取您想要的数字:
If u are lookin only to use sed then use TRE:
如果您只想使用 sed,请使用 TRE:
sed -n 's/.*\(0-9\)\(0-9\)\(0-9\).*/,,/g'.
I dint try and execute the above command so just make sure the syntax is right. Hope this helped.
我不会尝试执行上面的命令,因此只需确保语法正确即可。希望这有帮助。
回答by ghostdog74
using awk
使用 awk
linux$ echo -E "TestT100String" | awk '{gsub(/[^0-9]/,"")}1'
100
回答by ghostdog74
using just the bash shell
只使用 bash shell
declare -a array
i=0
while read -r line
do
case "$line" in
*TestT*String* )
while true
do
line=${line#*TestT}
array[$i]=${line%%String*}
line=${line#*String*}
i=$((i+1))
case "$line" in
*TestT*String* ) continue;;
*) break;;
esac
done
esac
done <"file"
echo ${array[@]}
回答by ephemient
I don't know why nobody ever uses expr
: it's portable and easy.
我不知道为什么从来没有人使用过expr
:它既便携又简单。
newName()
{
#Get input from function
newNameTXT=""
if num=`expr "$newNameTXT" : '[^0-9]*\([0-9]\+\)'`; then
echo "contains $num"
fi
}
回答by Fritz G. Mehner
Pure Bash. Use parameter substitution (no external processes and pipes):
纯猛击。使用参数替换(无外部进程和管道):
string="TestT100String"
echo ${string//[^[:digit:]]/}
Removes all non-digits.
删除所有非数字。
回答by ToastStudios
I Know this is an old topic but I came her along same searches and found another great possibility apply a regex on a String/Variable using grep:
我知道这是一个古老的话题,但我通过相同的搜索找到了她,并发现了另一种很好的可能性,即使用 grep 在字符串/变量上应用正则表达式:
# Simple
$(echo "TestT100String" | grep -Po "[0-9]{3}")
# More complex using lookaround
$(echo "TestT100String" | grep -Po "(?i)TestT\K[0-9]{3}(?=String)")
With using lookaround capabilities search expressions can be extended for better matching. Where (?i)
indicates the Pattern before the searched Pattern (lookahead),
\K
indicates the actual search pattern and (?=)
contains the pattern after the search (lookbehind).
使用环视功能可以扩展搜索表达式以获得更好的匹配。其中(?i)
表示搜索模式之前的模式(lookahead),
\K
表示实际搜索模式并(?=)
包含搜索后的模式(lookbehind)。
https://www.regular-expressions.info/lookaround.html
https://www.regular-expressions.info/lookaround.html
The given example matches the same as the PCRE regex TestT([0-9]{3})String
给定的示例与 PCRE 正则表达式匹配 TestT([0-9]{3})String