[Solved] Need Help in reading Response file

Hi All,

I have a requirement to read response file which looks like below

Ex:

NAME=SAM
DOB=01/01/1980
ADDRESS=
7658 James Street
NewYork
0000

Now, I need to take NAME, DOB, ADDRESS into variables

I am fine taking NAME and DOB

I need help on how can I take Multiple Lines into one variable?

Thanks
Kishore

Do you have any way to control how the response file is generated ?

If so, i suggest you to generate the ADDRESS like :

ADDRESS="7658 James Street
NewYork
0000"

or

ADDRESS="7658 James Street,NewYork,0000"

You can then just execut the response_file to load the variabes in your environment

in csh :

source ./response_file

in sh/ksh/bash:

. ./response_file

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~> _

Hi Wise,

Thanks for your Quick Reply

Sorry forgot to inform that

Address Varible might have Mutiple Lines. in Example i gave only 3 lines, what if we have a variable number of lines

Could you pl help me in that

Thanks

Just about to HTH...

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~> _

HTH, goodnight folks...

Please show us how your records are stacked. Does the next details start with NAME again or is it something else?

--ahamed

Hi Wise,

I am very thankful for the solution. I was able to tweak accordingly for my requirement.

I appreciate your extended help in weekend also.

Ahmed

My Actual Response file are having total 9 parameters.

8 parameters have only single Value.

9th Parameter (Above example I took Address) will have multiple lines (Varies from time to time)

I was able to understand what Wise gave and tweaked as per my requirements.

Thanks a lot for asking