Combining Quotes:

The Goal:

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 awk's field designators, $1 and $2.

Setting the Shell Variable:

name="Jacob Savage" (sh, bash and ksh)
set name = "Jacob Savage" (csh and tsh) 

The Line from the Datafile:

Jacob Savage:408-298-7732:934 La Barbara Dr. , San Jose,CA:02/27/78:500000 

The awk Command Line:

						awk -F: '$1 ~ /^'"$name"'/{print $2}' datafile
(Output)
408-298-7732
					

Step 1:

Test your knowledge of the Linux command at the command line before plugging in any shell variables.

						awk -F: '$1 ~ /^Jacob Savage/{print $2}' filename
(Output)
408-298-7732
					

Step 2:

Plug in the shell variable without changing anything ...

Get Linux Shells by Example 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.