简体   繁体   English

Grep变量模式匹配

[英]Grep variable pattern match

How can I get this to work? 我怎样才能让它发挥作用?

#!/bin/bash
SOMETHING=$(egrep '^  something' /some/dir/file.conf | awk -F '.' '{print $1}' | awk    '{print $2}')

if [ $SOMETHING = 123 ]; then
    echo "Found 123"
    else
    echo "Cannot find 123" && exit 1
fi

Results in grep complaining about a syntax error. 结果grep抱怨语法错误。 It doesn't like the '^ something' 它不喜欢'^某事'

Your multiple commands with pipes can be simply replaced with the awk itself. 使用管道的多个命令可以简单地用awk本身替换。 Use following script: 使用以下脚本:

SOMETHING=$(awk '/^ something/{print substr($4, 1, 3);}' somefile.conf)
if [ "$SOMETHING" = "123" ]; then
    echo "Found 123"
else
    echo "Cannot find 123" && exit 1  
fi

EDIT: Looks like you've edited the question and your script after I posted my anser. 编辑:看起来你在我发布我的anser之后编辑了问题和你的脚本。 Here is the modified awk command for you latest edit (don't do it again pls): 这是修改后的awk命令,供您最新编辑(请勿再次执行):

SOMETHING=$(awk -F "." '/^ something/{split($1, a, " "); print a[2]}' somefile.conf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM