Unix Shell - using cfg file to read certain values

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > using cfg file to read certain values





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author using cfg file to read certain values
stevenbentley@yahoo.com

2005-03-25, 6:03 pm

Hi,
I'm using a cfg file that looks like the following:

###########
#
TOP_LABEL = 1234

SUB_LABEL_1 :
file.1 : REPLACE TAG OLDTXT NEWTXT
file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
END

SUB_LABEL_2 :
file.1 : REPLACE : TAG OLDTXT NEWTXT
file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
END

###########
#
TOP_LABEL = 5678

SUB_LABEL_1 :
file.1 : REPLACE TAG OLDTXT NEWTXT
file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
END

SUB_LABEL_2 :
file.1 : REPLACE : TAG OLDTXT NEWTXT
file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
END

My script is called with for example MyScript 1234 SUB_LABEL_1
Now the script should use the cfg file to search for the commands to
perform based on this configuation. In the example above it should
perfrom the two instructions on file.1 for the top most label.
So far I can read each line in the cfg file into a variable but I'd
like to search for TOP_LABEL and 'then' start reading the lines.
I was thinking of perhaps using fgrep to find "TOP_LABEL = 1234" but I
don't know how to then 'after' this line start reading in the following
commands. I also have the idea that the END tag can be used to stop
reading the lines in the sub level but perhaps this is not required.
What would be your suggestions as to the cleanest way to do this? Is
there a better way than just reading each line from the top of the file
and checking if this is the TOP_LABEL that I am interetsed in? I also
have ideas using intermediate files but I would prefer not to do this.

Many Thanks for your time
Steve

BTW: Comments on the structure of the cfg file are most welcome
PS: How do i top my full email being displayed?

Chris F.A. Johnson

2005-03-25, 6:03 pm

On Fri, 25 Mar 2005 at 16:01 GMT, stevenbentley@yahoo.com wrote:
> Hi,
> I'm using a cfg file that looks like the following:
>
> ###########
> #
> TOP_LABEL = 1234
>
> SUB_LABEL_1 :
> file.1 : REPLACE TAG OLDTXT NEWTXT
> file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> SUB_LABEL_2 :
> file.1 : REPLACE : TAG OLDTXT NEWTXT
> file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> ###########
> #
> TOP_LABEL = 5678
>
> SUB_LABEL_1 :
> file.1 : REPLACE TAG OLDTXT NEWTXT
> file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> SUB_LABEL_2 :
> file.1 : REPLACE : TAG OLDTXT NEWTXT
> file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> My script is called with for example MyScript 1234 SUB_LABEL_1
> Now the script should use the cfg file to search for the commands to
> perform based on this configuation. In the example above it should
> perfrom the two instructions on file.1 for the top most label.
> So far I can read each line in the cfg file into a variable but I'd
> like to search for TOP_LABEL and 'then' start reading the lines.
> I was thinking of perhaps using fgrep to find "TOP_LABEL = 1234" but I
> don't know how to then 'after' this line start reading in the following
> commands. I also have the idea that the END tag can be used to stop
> reading the lines in the sub level but perhaps this is not required.
> What would be your suggestions as to the cleanest way to do this? Is
> there a better way than just reading each line from the top of the file
> and checking if this is the TOP_LABEL that I am interetsed in? I also
> have ideas using intermediate files but I would prefer not to do this.


x=0
while IFS= read -r line
do
case $line in
TOP_LABEL*=*"$1"*) x=1 ;;
END*) break ;;
*)
if [ $x -ge 1 ]
then
case $line in
$2*) x=2
continue
;;
esac
if [ $x -eq 2 ]
then
## echo $line ## uncommment to check that the right
## lines are used
: do something with $line
fi
fi
;;
esac
done < $HOME/txt

> BTW: Comments on the structure of the cfg file are most welcome
> PS: How do i top my full email being displayed?


Without knowing more about what you are trying to do, it's hard to
give advice. At a glance, this appears to be more complicated than
it needs to be.


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
stevenbentley@yahoo.com

2005-03-25, 6:03 pm

Hi, thanks for the quick reply.

Let me try to explain in more detail what I'm trying to do. I would
like to give two parameters into my script, that the script will use in
order to determine which actions to then perform.
Therefore if i give
MyScript 1234 SUB_LABEL_1 . The script will search the cfg file for a
TOP_LEVEL that equals 1234. It should then search within this TOP_LABEL
for the correct SUB_LABEL. Once this has been identified, I would like
a Var(s) to hold the lines that belong to this SUB_LABEL for processing
further. So in this example:

Var1=file.1 : REPLACE TAG OLDTXT NEWTXT
Var2=file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL

If the script is run with MyScript 5678 SUB_LABEL_2 then the Vars would
be:
Var1=file.1 : REPLACE : TAG OLDTXT NEWTXT
Var2=file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL

I have problems understanding the example you give. I tried it but it
didn't seem to work however to be honest I need help to understand it.
Would you mind commenting it for us beginners?

Thanks
Steve

Chris F.A. Johnson

2005-03-25, 6:03 pm

On Fri, 25 Mar 2005 at 18:49 GMT, stevenbentley@yahoo.com wrote:
> Hi, thanks for the quick reply.


Pleae quote relevant sections of the post you are replying to. You
may be reading this via Google groups, but you are actually posting
to a usenet newsgroup; previous posts may or may not be readily
available.

> Let me try to explain in more detail what I'm trying to do. I would
> like to give two parameters into my script, that the script will use in
> order to determine which actions to then perform.
> Therefore if i give
> MyScript 1234 SUB_LABEL_1 . The script will search the cfg file for a
> TOP_LEVEL that equals 1234. It should then search within this TOP_LABEL
> for the correct SUB_LABEL. Once this has been identified, I would like
> a Var(s) to hold the lines that belong to this SUB_LABEL for processing
> further. So in this example:
>
> Var1=file.1 : REPLACE TAG OLDTXT NEWTXT
> Var2=file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
>
> If the script is run with MyScript 5678 SUB_LABEL_2 then the Vars would
> be:
> Var1=file.1 : REPLACE : TAG OLDTXT NEWTXT
> Var2=file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL


This doesn't give much more information than your previous post.
Will there always be two lines?

> I have problems understanding the example you give. I tried it but it
> didn't seem to work


What does "didn't seem to work" mean? What actaully happened? Did
you try with different arguments?

> however to be honest I need help to understand it.
> Would you mind commenting it for us beginners?


config_file=$HOME/txt ## path to your config file; adjust to taste
flag=0 ## flag to indicate when in section you want

while IFS= read -r line ## read the file line by line
do
case $line in ## Check the contents of each line

## if it matches the first argument, set the flag to 1
TOP_LABEL*=*"$1"*) flag=1 ;;

## if you are in the block you want,
## stop when you reach a line beginning with end
## (this line has been fixed from my previous post)
END*) [ $flag -eq 2 ] && break ;;

*) ## For all other lines
if [ $flag -ge 1 ] ## If a flag has been set, check for 2nd argument
then
case $line in
$2*) flag=2 ## line begins with 2nd argument; set flag and
continue ## go on to the next line
;;
esac
if [ $flag -eq 2 ] ## both flags are set
then
## If this block gets executed, it is because
## you are in the section of the file you want
## here you do whatever you want with the lines

## In bash or ksh you could put them in an array:
array[${#array[@]}]=$line

fi
fi
;;
esac
done < "$config_file"


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
stevenbentley@yahoo.com

2005-03-25, 6:03 pm

> Pleae quote relevant sections of the post you are replying to. You
> may be reading this via Google groups, but you are actually

posting
> to a usenet newsgroup; previous posts may or may not be readily
> available.
>

Point taken

use in[vbcol=seagreen]
a[vbcol=seagreen]
TOP_LABEL[vbcol=seagreen]
like[vbcol=seagreen]
processing[vbcol=seagreen]
would[vbcol=seagreen]
>
> This doesn't give much more information than your previous post.
> Will there always be two lines?
>

No there can be no lines to process or multiple ones

it[vbcol=seagreen]
>
> What does "didn't seem to work" mean? What actaully happened? Did
> you try with different arguments?
>

I tried with the code from your last post and this 'did seem to work'

I've tried with different input parameters and also adding/deleting
entries and this works super.

>
> config_file=$HOME/txt ## path to your config file; adjust to taste
> flag=0 ## flag to indicate when in section you want
>
> while IFS= read -r line ## read the file line by line
> do
> case $line in ## Check the contents of each line
>
> ## if it matches the first argument, set the flag to 1
> TOP_LABEL*=*"$1"*) flag=1 ;;
>
> ## if you are in the block you want,
> ## stop when you reach a line beginning with end
> ## (this line has been fixed from my previous post)
> END*) [ $flag -eq 2 ] && break ;;
>
> *) ## For all other lines
> if [ $flag -ge 1 ] ## If a flag has been set, check for 2nd

argument
> then
> case $line in
> $2*) flag=2 ## line begins with 2nd argument;

set flag and
> continue ## go on to the next line
> ;;
> esac
> if [ $flag -eq 2 ] ## both flags are set
> then
> ## If this block gets executed, it is because
> ## you are in the section of the file you want
> ## here you do whatever you want with the lines
>
> ## In bash or ksh you could put them in an array:
> array[${#array[@]}]=$line
>
> fi
> fi
> ;;
> esac
> done < "$config_file"
>

It was a big help for me to understand.

Thanks

stevenbentley@yahoo.com

2005-03-30, 7:55 am

Hi, thanks for the quick reply.

Let me try to explain in more detail what I'm trying to do. I would
like to give two parameters into my script, that the script will use in
order to determine which actions to then perform.
Therefore if i give
MyScript 1234 SUB_LABEL_1 . The script will search the cfg file for a
TOP_LEVEL that equals 1234. It should then search within this TOP_LABEL
for the correct SUB_LABEL. Once this has been identified, I would like
a Var(s) to hold the lines that belong to this SUB_LABEL for processing
further. So in this example:

Var1=file.1 : REPLACE TAG OLDTXT NEWTXT
Var2=file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL

If the script is run with MyScript 5678 SUB_LABEL_2 then the Vars would
be:
Var1=file.1 : REPLACE : TAG OLDTXT NEWTXT
Var2=file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL

I have problems understanding the example you give. I tried it but it
didn't seem to work however to be honest I need help to understand it.
Would you mind commenting it for us beginners?

Thanks
Steve

Chris F.A. Johnson

2005-03-30, 7:55 am

On Fri, 25 Mar 2005 at 18:49 GMT, stevenbentley@yahoo.com wrote:
> Hi, thanks for the quick reply.


Pleae quote relevant sections of the post you are replying to. You
may be reading this via Google groups, but you are actually posting
to a usenet newsgroup; previous posts may or may not be readily
available.

> Let me try to explain in more detail what I'm trying to do. I would
> like to give two parameters into my script, that the script will use in
> order to determine which actions to then perform.
> Therefore if i give
> MyScript 1234 SUB_LABEL_1 . The script will search the cfg file for a
> TOP_LEVEL that equals 1234. It should then search within this TOP_LABEL
> for the correct SUB_LABEL. Once this has been identified, I would like
> a Var(s) to hold the lines that belong to this SUB_LABEL for processing
> further. So in this example:
>
> Var1=file.1 : REPLACE TAG OLDTXT NEWTXT
> Var2=file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
>
> If the script is run with MyScript 5678 SUB_LABEL_2 then the Vars would
> be:
> Var1=file.1 : REPLACE : TAG OLDTXT NEWTXT
> Var2=file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL


This doesn't give much more information than your previous post.
Will there always be two lines?

> I have problems understanding the example you give. I tried it but it
> didn't seem to work


What does "didn't seem to work" mean? What actaully happened? Did
you try with different arguments?

> however to be honest I need help to understand it.
> Would you mind commenting it for us beginners?


config_file=$HOME/txt ## path to your config file; adjust to taste
flag=0 ## flag to indicate when in section you want

while IFS= read -r line ## read the file line by line
do
case $line in ## Check the contents of each line

## if it matches the first argument, set the flag to 1
TOP_LABEL*=*"$1"*) flag=1 ;;

## if you are in the block you want,
## stop when you reach a line beginning with end
## (this line has been fixed from my previous post)
END*) [ $flag -eq 2 ] && break ;;

*) ## For all other lines
if [ $flag -ge 1 ] ## If a flag has been set, check for 2nd argument
then
case $line in
$2*) flag=2 ## line begins with 2nd argument; set flag and
continue ## go on to the next line
;;
esac
if [ $flag -eq 2 ] ## both flags are set
then
## If this block gets executed, it is because
## you are in the section of the file you want
## here you do whatever you want with the lines

## In bash or ksh you could put them in an array:
array[${#array[@]}]=$line

fi
fi
;;
esac
done < "$config_file"


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
stevenbentley@yahoo.com

2005-03-30, 7:55 am

> Pleae quote relevant sections of the post you are replying to. You
> may be reading this via Google groups, but you are actually

posting
> to a usenet newsgroup; previous posts may or may not be readily
> available.
>

Point taken

use in[vbcol=seagreen]
a[vbcol=seagreen]
TOP_LABEL[vbcol=seagreen]
like[vbcol=seagreen]
processing[vbcol=seagreen]
would[vbcol=seagreen]
>
> This doesn't give much more information than your previous post.
> Will there always be two lines?
>

No there can be no lines to process or multiple ones

it[vbcol=seagreen]
>
> What does "didn't seem to work" mean? What actaully happened? Did
> you try with different arguments?
>

I tried with the code from your last post and this 'did seem to work'

I've tried with different input parameters and also adding/deleting
entries and this works super.

>
> config_file=$HOME/txt ## path to your config file; adjust to taste
> flag=0 ## flag to indicate when in section you want
>
> while IFS= read -r line ## read the file line by line
> do
> case $line in ## Check the contents of each line
>
> ## if it matches the first argument, set the flag to 1
> TOP_LABEL*=*"$1"*) flag=1 ;;
>
> ## if you are in the block you want,
> ## stop when you reach a line beginning with end
> ## (this line has been fixed from my previous post)
> END*) [ $flag -eq 2 ] && break ;;
>
> *) ## For all other lines
> if [ $flag -ge 1 ] ## If a flag has been set, check for 2nd

argument
> then
> case $line in
> $2*) flag=2 ## line begins with 2nd argument;

set flag and
> continue ## go on to the next line
> ;;
> esac
> if [ $flag -eq 2 ] ## both flags are set
> then
> ## If this block gets executed, it is because
> ## you are in the section of the file you want
> ## here you do whatever you want with the lines
>
> ## In bash or ksh you could put them in an array:
> array[${#array[@]}]=$line
>
> fi
> fi
> ;;
> esac
> done < "$config_file"
>

It was a big help for me to understand.

Thanks

Chris F.A. Johnson

2005-03-30, 8:47 pm

On Fri, 25 Mar 2005 at 16:01 GMT, stevenbentley@yahoo.com wrote:
> Hi,
> I'm using a cfg file that looks like the following:
>
> ###########
> #
> TOP_LABEL = 1234
>
> SUB_LABEL_1 :
> file.1 : REPLACE TAG OLDTXT NEWTXT
> file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> SUB_LABEL_2 :
> file.1 : REPLACE : TAG OLDTXT NEWTXT
> file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> ###########
> #
> TOP_LABEL = 5678
>
> SUB_LABEL_1 :
> file.1 : REPLACE TAG OLDTXT NEWTXT
> file.1 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> SUB_LABEL_2 :
> file.1 : REPLACE : TAG OLDTXT NEWTXT
> file.2 : REPLACE_ALL OLD_LABEL NEW_LABEL
> END
>
> My script is called with for example MyScript 1234 SUB_LABEL_1
> Now the script should use the cfg file to search for the commands to
> perform based on this configuation. In the example above it should
> perfrom the two instructions on file.1 for the top most label.
> So far I can read each line in the cfg file into a variable but I'd
> like to search for TOP_LABEL and 'then' start reading the lines.
> I was thinking of perhaps using fgrep to find "TOP_LABEL = 1234" but I
> don't know how to then 'after' this line start reading in the following
> commands. I also have the idea that the END tag can be used to stop
> reading the lines in the sub level but perhaps this is not required.
> What would be your suggestions as to the cleanest way to do this? Is
> there a better way than just reading each line from the top of the file
> and checking if this is the TOP_LABEL that I am interetsed in? I also
> have ideas using intermediate files but I would prefer not to do this.


x=0
while IFS= read -r line
do
case $line in
TOP_LABEL*=*"$1"*) x=1 ;;
END*) break ;;
*)
if [ $x -ge 1 ]
then
case $line in
$2*) x=2
continue
;;
esac
if [ $x -eq 2 ]
then
## echo $line ## uncommment to check that the right
## lines are used
: do something with $line
fi
fi
;;
esac
done < $HOME/txt

> BTW: Comments on the structure of the cfg file are most welcome
> PS: How do i top my full email being displayed?


Without knowing more about what you are trying to do, it's hard to
give advice. At a glance, this appears to be more complicated than
it needs to be.


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com