C.5. Setting the Shell Variable

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

Step1:

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

Step 2:

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, ...

Get UNIX® Shells by Example, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.