Output on same line while loop using variable

Currently using below script but echo it print the output in two line.

Input file all-vm-final-2.txt

CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4
CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4
CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4
DEALA08762 SDDC_LDC DS-SIO-Workload-NonPROD-01 4
 while IFS=" ," read a b c d; do
    echo "New-HardDisk -VM  $a -CapacityGB $d -DiskType Flat -Datastore $c -StorageFormat Thin" >> script-file.txt
done < all-vm-final-2.txt

I am getting output like this

New-HardDisk -VM  CEALA08893 -CapacityGB 4
 -DiskType Flat -Datastore DS_SIO_Workload_SAPUI_UAT_01 -StorageFormat Thin
New-HardDisk -VM  CEALA09546 -CapacityGB 4
 -DiskType Flat -Datastore DS-SIO-PD5_Workload_UAT_SP1_Flash_07 -StorageFormat Thin
New-HardDisk -VM  CEALA09702 -CapacityGB 4
 -DiskType Flat -Datastore DS-VSAN-RMP-WORKLOAD01 -StorageFormat Thin
New-HardDisk -VM  DEALA08762 -CapacityGB 4
 -DiskType Flat -Datastore DS-SIO-Workload-NonPROD-01 -StorageFormat Thin

On what OS was your input file produced? Looks like MSsomething... and so I would not be surprised that "$d" contains next line char...
Try and put

-CapacityGB $d

at the end and see what happens...
So for testing new code:

 while IFS=" ," read a b c d; do
    echo "New-HardDisk -VM  $a  -DiskType Flat -Datastore $c -StorageFormat Thin -CapacityGB $d" >> script-file.txt
done < all-vm-final-2.txt

If that is the case, then pass your file through dos2unix type utility...

My 2 cents...

1 Like

Pls post a hexdump of your input file.

I am using cygwin in windows. Thanks it was working now i used

-CapacityGB $d 

at the end

You might be better forcing the formatting with printf rather that echo, such that your output statement would be more like:-

do
   printf "New-HardDisk -VM  $a -CapacityGB $d -DiskType Flat -Datastore $c -StorageFormat Thin\n" "$a" "$d" "$c"
done

I would also suggest that you don't append every output statement to a file, but write all the output from your loop to the file in one go, so done < input_file > output_file

Does this help at all?
Robin

1 Like