CONTENTS |
Precedes a character and escapes that character.
Same as putting single quotes around one character.
Must be matched.
Protect all metacharacters from interpretation except the following:
Itself.
Exclamation point (csh only).
Backslash.
C Shell | Bourne Shell | Korn Shell |
echo '$><%^&*' | echo '$*&!><?' | echo '$*&!><?' |
echo 'I need $5.00\!' | echo 'I need \$5.00!' | echo 'I need \$5.00!' |
ho 'She cried, "Help"' | echo 'She cried, "Help"' | echo 'She cried, "Help"' |
echo '\\\\' | echo '\\\\' | print '\\\\' |
\\\\ | \\ | \\ |
Must be matched.
Protect all metacharacters from interpretation except the following:
Itself.
Exclamation point (csh only).
$ used for variable substitution.
' ' Backquotes for command substitution.
C Shell | Bourne Shell | Korn Shell |
---|---|---|
echo "Hello $LOGNAME\!" | echo "Hello $LOGNAME!" | print "Hello $LOGNAME!" |
echo "I don't care" | echo "I don't care" | print "I don't care" |
echo "The date is 'date'" | echo "The date is 'date'" | print "The date is $(date)" |
echo "\\\\" | echo "\\\\" | print "\\\\" |
\\\\ | \ | \ |
The end result is to be able to embed the shell variable in the awk command line and have the shell expand the variable without interfering with nawk's field designators, $1 and $2.
name="Jacob Savage" (Bourne and Korn Shell)
set name = "Jacob Savage" ( C Shell )
(The line from the datafile)
Jacob Savage:408-298-7732:934 La Barbara Dr. , San Jose, CA:02/27/78:500000
(The nawk command line)
nawk -F: '$1 ~ /^' "$name" '/{print $2}' datafile
(Output)
408-298-7732
Test your knowledge of the UNIX command at the command line before plugging in any shell variables.
nawk -F: '$1 ~ /^Jacob Savage/{print $2}' filename
(Output)
408-298-7732
Plug in the shell variable without changing anything else. Leave all quotes as they were.
nawk -F: '$1 ~ /^$name/{print $2}' datafile
Starting at the left-hand side of the awk command leave the first quote as is; right before the shell dollar sign in $name, place another single quote. Now the first quote is matched and all text within these two quotes is protected from shell interference. The variable is exposed. Now put another single quote right after the e in $name. This starts another matched set of single quotes ending after awk's closing curly brace. Everything within this set of quotes is also protected from shell interpretation.
Enclose the shell variable in a set of double quotes. This allows the variable to be expanded but the value of the variable will be treated as single string if it contains whitespace. The whitespace must be protected so that the command line is parsed properly.
Count the number of quotes. There should be an even number of single quotes and an even number of double quotes.
oldname="Ellie Main"
newname="Eleanor Quigley"
Make sure the command works.
nawk -F: '/^Ellie Main/{$1="Eleanor Quigley"; print $0}' datafile
Plug in the variables.
nawk -F: '/^$oldname/{$1="$newname"; print $0}' datafile
Play the quoting game. Starting at the first single quote at the left, move across the line until you come to the variable $oldname and place another single quote just before the dollar sign. Put another single quote right after the last letter in the variable name.
Now move to the right and place another single quote right before the dollar sign in $newname. Put another single quote after the last character in $newname.
Count the number of single quotes. If the number of single quotes is an even number, each quote has a matching quote. If not, you have forgotten a step.
Enclose each of the shell variables in double quotes. The double quotes are placed snugly around the shell variable.
CONTENTS |