URGENT: Script/Function needed to read text property files in block wise

Hi,

Iam in a need for a script/function in KSH where I want to read a text file (property file) in block by block. Here is the example:

Heading Name Descripton

Block Block1 Value1 Description
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value

Block Block2 Value2 Description
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value
Property Name Value

As per the above example, my requirement is similar to this. I want to read entire bock including its name, values and process them, along with them I want to read their Property Names/Values and create under that Block.

The main requirement is, no other block's Property's should be mingled/interfered with other Block's Properties.

Any help is greatly appreciated.

Show what you have come up with so far

Is there any separator between 2 blocks? Or do you have something special to identify the beginning of a block? If I knew it began by "Block", say, I would write in sh something like

function block () {
local l
local b=0

read line
while [ "$line" ]; do
case "$line" in 
   Block*\) b=$\(\(b\+1\)\)
               echo "beginning of block $b"
               l=1 ;;
       *\) echo "line $l of block $b"
          l=$\(\(l\+1\)\)
esac
read line
done

}

Should work in ksh too.

Hi,

Thanks for the reply. But in your script function, where do we refer to the text file from where we are reading the input. Can you explain in little bit more please.

The function reads from stdin. Assuming you saved my code in a /tmp/script file, and that you wish to read a file nammed foo.txt you need to source /tmp/script, and then pipe foo.txt to the function. Namely:

source /tmp/script
block < foo.txt

or: cat foo.txt | block

BTW, my function is not ksh compatible, but bash compatible. But could properly read a file like this:

Block b1
hello line1
world line2
Block b2
hi line3
again line4
end line5

Hi, I have fileds like
uname="test"
pwd="ujeua"

in host.properties file.

I want to read these values in a sh file.

for i in `ls ${directory}/chk.properties
do

------how to get values here--------------

done

Thanks

Even I had a similar requirement.
Have got a way to read the properties with below command

. cfg/props.properties

but am getting the below error for every blank line

: command not found: line 8:
: command not found: line 10:
: command not found: line 12:

Can any one help me removing the blank spaces before reading the properties file? Or is there any better way to read property files?

Thanks in advance,
Surendra

Hi,
Here is the sample example.

CD=`pwd`
for i in `ls ${CD}/check.properties`
do
echo "in for loop..."
. ${i}
echo "User Name is $USER_NAME and pass word is $PASS_WORD "
done

Am getting the values correctly.