Processing a text file with spaces in the data and a file separator

I needed to code a script that would read through a text file and process some data. The format of the file was as follows:

Item Id|Item Name
01|Car
02|Bus
03|Plane

The code was:

#!/usr/bin/ksh
for parm_line in `cat dean_test.txt`
do
    export item_id=$(echo $parm_line | cut -f1 -d\|)
    export item_name=$(echo $parm_line | cut -f2 -d\|)
 
    if  [ $item_id != "Item Id" ]; then
        echo $parm_line
    fi
done

My expectation was that it would skip the header line and process the three data lines. The output looked like:

Item
Id|Item
Name
01|Car
02|Bus
03|Plane

This was being caused by the fact that the header row had spaces in the names of the fields. I had to change the code as follows and add the IFS:

#!/usr/bin/ksh
 
IFS=$'\n' 
 
for parm_line in `cat dean_test.txt`
do
    export item_id=$(echo $parm_line | cut -f1 -d\|)
    export item_name=$(echo $parm_line | cut -f2 -d\|)
 
    if  [ $item_id != "Item Id" ]; then
        echo $parm_line
    fi
done

This change fixed the issues and I got the correct output:

01|Car
02|Bus
03|Plane

The short snippet of code was part of a much larger script. I could not use a WHILE loop as I was going to be issuing ssh commands.

Author: Dean Capps

Database consultant at Amazon Web Services.

142
51
73
77