LONGHAND, using OSX 10.7.5, default bash terminal...
If the response file is small like your example then this works...
Note that a comma is deliberately added just for better appearance; it could be
a newline if you wish...
#/bin/bash
# multi_vars.sh
ifs_str="$IFS"
IFS="
"
echo 'NAME=SAM
DOB=01/01/1980
ADDRESS=
7658 James Street
NewYork
0000' > /tmp/multi_vars.txt
read -d '' -r text < /tmp/multi_vars.txt
echo ""
echo "$text"
echo ""
echo "Start of placing substrings into variables..."
echo ""
var_array=($text)
n=0
while true
do
if [ $n -ge ${#var_array[@]} ]
then
break
fi
subtext="${var_array[$n]}"
if [ "${subtext:0:4}" == "NAME" ]
then
name=${var_array[0]}
fi
if [ "${subtext:0:3}" == "DOB" ]
then
dob=${var_array[1]}
fi
if [ "${subtext:0:7}" == "ADDRESS" ]
then
# Added a comma just for added completeness... ;o)
address="${var_array[$n]}${var_array[$[ ( $n + 1 ) ]]}, ${var_array[$[ ( $n + 2 ) ]]}"
fi
number="${var_array[$n]}"
n=$[ ( $n + 1 ) ]
done
echo "$name"
echo "$dob"
echo "$address"
echo "$number"
echo ""
echo "DONE!"
echo ""
IFS="$ifs_str"
exit 0
Results:-
Last login: Sun Mar 2 19:29:52 on ttys000
AMIGA:barrywalker~> ./multi_vars.sh
NAME=SAM
DOB=01/01/1980
ADDRESS=
7658 James Street
NewYork
0000
Start of placing substrings into variables...
NAME=SAM
DOB=01/01/1980
ADDRESS=7658 James Street, NewYork
0000
DONE!
AMIGA:barrywalker~> _
What will be the maximum number of lines that ADDRESS will span?
I only need the maximum at this point...
Will continue tomorrow.
HTH.
---------- Post updated at 10:58 PM ---------- Previous update was at 10:38 PM ----------
Decided to stay up and quickly do a test piece...
This should take into account multiple newlines in the ADDRESS section:-
#/bin/bash
# multi_vars.sh
ifs_str="$IFS"
IFS="
"
echo 'NAME=SAM
DOB=01/01/1980
ADDRESS=
7658 James Street
NewYork
0000' > /tmp/multi_vars.txt
read -d '' -r text < /tmp/multi_vars.txt
echo ""
echo "$text"
echo ""
echo "Start of placing substrings into variables..."
echo ""
var_array=($text)
n=0
while true
do
if [ $n -ge ${#var_array[@]} ]
then
break
fi
subtext="${var_array[$n]}"
if [ "${subtext:0:4}" == "NAME" ]
then
name=${var_array[0]}
fi
if [ "${subtext:0:3}" == "DOB" ]
then
dob=${var_array[1]}
fi
if [ "${subtext:0:7}" == "ADDRESS" ]
then
address=$address${var_array[2]}
for m in $( seq 3 1 $[ ( ${#var_array[@]} - 2 ) ] )
do
address=$address"${var_array[$m]}. "
done
fi
number=${var_array[$n]}
n=$[ ( $n + 1 ) ]
done
echo "$name"
echo "$dob"
echo "$address"
echo "$number"
echo ""
echo "DONE!"
echo ""
IFS="$ifs_str"
exit 0
Results:-
Last login: Sun Mar 2 22:39:26 on ttys000
AMIGA:barrywalker~> ./multi_vars.sh
NAME=SAM
DOB=01/01/1980
ADDRESS=
7658 James Street
NewYork
0000
Start of placing substrings into variables...
NAME=SAM
DOB=01/01/1980
ADDRESS=7658 James Street. NewYork.
0000
DONE!
AMIGA:barrywalker~> _