0

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'

4
  • 1
    What do you want it to match, exactly? Commented Jul 2, 2013 at 11:32
  • 1
    edit your question with examples please. grep|awk|cut should not the best way to go. Commented Jul 2, 2013 at 11:34
  • mv SOMETHING=$(egrep '^ something' somefile.conf | awk '{print $4}' | cut -c1-3) above the test, and then just put $SOMETHING in the test. Commented Jul 2, 2013 at 11:36
  • UUO-G Commented Jul 2, 2013 at 11:56

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.