ibash
ibash

Reputation: 741

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'

Upvotes: 0

Views: 821

Answers (1)

anubhava
anubhava

Reputation: 785196

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)

Upvotes: 2

Related Questions