bash 如何使用awk确定字符串是否包含子字符串

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

How to determine if string contains substring with awk

linuxbashshellawk

提问by Steeve007

I have the following bash script segment that uses grep to find out whether input line contains certain pattern or not.

我有以下 bash 脚本段,它使用 grep 找出输入行是否包含特定模式。

echo $input_line | grep -q "$pattern"
if [ $? -eq 0 ];
then
    # Input line contains pattern
    # Execute some bash commands
else
    # Input line DOES NOT contain pattern
    # Execute other bash commands
fi

I need to rewrite it with awk without grep. I tried several variants, but none of them works, for example, both of the following always print MATCH, no matter contains input line pattern or not.

我需要在没有 grep 的情况下用 awk 重写它。我尝试了几种变体,但它们都不起作用,例如,以下两个总是打印 MATCH,无论是否包含输入线模式。

echo $input_line | awk -v b="$pattern" '/pattern/ { print "MATCH" }'

echo $input_line | awk -v b="$pattern" '/
pattern="abc"  
input_line="xyz123 678q we uqa abc asd"  
RES=$(echo $input_line | awk -v pat="$pattern" '
Input line xyz123 678q we uqa abc asd contains pattern abc  
./test.sh: line 21: [: -eq: unary operator expected  
Input line tult uil7665 5444tu l098 7tu DOES NOT contains pattern abc  
~ pat { print "1" }') if [ $RES -eq 1 ]; then echo Input line $input_line contains pattern $pattern else echo Input line $input_line DOES NOT contains pattern $pattern fi input_line="tult uil7665 5444tu l098 7tu" RES=$(echo $input_line | awk -v pat="$pattern" '
if [[ "$(input_line | awk '/'$pattern'/ { print "MATCH" }')" == "MATCH" ]]
then
         pattern matches
else
         No pattern matches
fi
~ pat { print "1" }') if [ $RES -eq 1 ]; then echo Input line $input_line contains pattern $pattern else echo Input line $input_line DOES NOT contains pattern $pattern fi
~ pattern/ { print "MATCH" }'

Update:

更新:

Removing / as recommended resolved the described issue. Now I have the problem with returning result of awk command for usage in if statement. I wrote the following "ugly" snippet:

删除 / 按照建议解决了所描述的问题。现在我在返回 awk 命令的结果以供在 if 语句中使用时遇到了问题。我写了以下“丑陋”的片段:

##代码##

So I've got the following output, because in second case when input line doesn't contain pattern the variable RES is not set:

所以我有以下输出,因为在第二种情况下,当输入行不包含模式时,变量 RES 未设置:

##代码##

Any ideas how to improve this "ugly" script snippet with if statement?
Thanks

任何想法如何使用 if 语句改进这个“丑陋”的脚本片段?
谢谢

回答by Raman Sailopal

To use awk with a referenced variable for the pattern match you would need to do:

要将 awk 与模式匹配的引用变量一起使用,您需要执行以下操作:

##代码##

I'm assuming here that the regular expression is a dynamic variable referenced by the variable pattern.

我在这里假设正则表达式是一个由变量模式引用的动态变量。

Place particular attention to the position of the single quotes in the awk statement.

特别注意单引号在 awk 语句中的位置。