AWK Formatting Problem

Hi All,

I'm having a problem with the way awk is interperting a space between double quotes in a for loop. Below is the code and output from running the script:

AWK for loop:
for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{
print "\n--------------------------------"}' < "edgewater-temps.csv" )
do
echo $i
done

Script output:
Probe
Name:CRAC-02A
Probe
Temp:73.0
--------------------------------
Probe
Name:CRAC-01
Probe
Temp:61.8
--------------------------------
Probe
Name:CRAC-12
Probe
Temp:67.8
--------------------------------
Probe
Name:CRAC-02
Probe
Temp:66.7
--------------------------------

Expected output:
Probe Name:CRAC-02A
Probe Temp:73.0
--------------------------------
Probe Name:CRAC-01
Probe Temp:61.8
--------------------------------
Probe Name:CRAC-12
Probe Temp:67.8
--------------------------------
Probe Name:CRAC-02
Probe Temp:66.7
--------------------------------

Question: Why is awk creating a newline for a space between double quotes?

Any help would be greatly apreciated.

Thanks,

Charles

it's a not 'awk' introducing an issue here - it's the surrounding 'for' loop parsing the output from 'awk'.
The 'for' loop iterates through through the list (specified in the 'in' clause) using the InternalFieldSeparator (aka IFS). By default the IFS is set to space, tab and new-line.
Your awk produces lines with the embedded spaces 'Probe Name' and 'Probe Temp'. The 'for' loop treats those embedded space and field separators - therefore you get new lines with your 'echo'.

You can either change your awk removing the embedded spaces from its output like so 'Probe Name' -> 'ProbeName' and leave the rest of the code unchanged.
Reset the IFS to something that you wouldn't expect from awk's output. Something along these lines:

#!/bin/ksh

IFS='^'; for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{
print "\n--------------------------------"}' < "edgewater-temps.csv" )
do
   echo $i
done

BTW, why are you printing the '\n---' in awk? Would it be more intuitive to do it in the wrapper loop AFTER the 'echo' is done?

The alternative would be to do it all in shell

#!/bin/ksh

while IFS=, read name temp junk
do
   echo "Probe Name: ${name}"
   echo "Probe Temp: ${temp}"
   echo '--------------------'
done < "edgewater-temps.csv"

Pick your poison! (as someone recently said).

Thank you very much for you help. Both the for and while loops worked great.