Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have this command line that I enter into terminal and it works as intended:

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID: / {if ($2 > 500) print name}'

What I want to do is use sh -c "insert command string here" and when I try and use the above statement, it gives me these errors:

awk: syntax error at source line 1
 context is
    /^RecordName:/ >>>  {name=} <<< 
awk: illegal statement at source line 1
awk: illegal statement at source line 1

Any idea how I would correct this? I need it for a program in objective-c.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Single quotes don't prevent variable expansion inside double quotes:

$ echo "a'$RANDOM'"
a'23976'

You could replace $ with \$ or ' with '\'':

$ sh -c "echo a b | awk '{print \$2}'"
b
$ sh -c 'echo a b | awk '\''{print $2}'\'
b

Or use a heredoc:

sh -s <<'END'
echo a b | awk '{print $2}'
END
share|improve this answer
    
Perfect, thank you so much. –  John Oct 10 '12 at 23:00
add comment

Your awk command does not have a closing '.

share|improve this answer
    
My mistake, I just forgot to add that to the question but had it in my code. –  John Oct 10 '12 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.