简体   繁体   中英

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. It doesn't like the '^ something'

Your multiple commands with pipes can be simply replaced with the awk itself. 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. Here is the modified awk command for you latest edit (don't do it again pls):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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