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.
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
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