Unix Shell - A simple Korn shell question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > A simple Korn shell question





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 A simple Korn shell question
Professor chaos

2005-02-23, 8:47 pm

Hello all,

I am new to korn shell scripting and i am having difficulty trying to
code this..

I have a programm which when executed prints out the following..

item1: somevalue somevalue somevalue somevalue somevalue
property: value1
item2: somevalue somevalue somevalue somevalue somevalue
property: value2
item3: somevalue somevalue somevalue somevalue somevalue
property: value3

Now i am writing another script which wud call the first script and
take the following output into and variable say $foo.

for each item i want to know what the value of the property is..

i have to do this using korn shell script. In that i can call awk. but
i cannot use perl.

My approach is take an array and put all the lines in an array and
parse for 'item' and if found parse for property in next line and take
value.

But when i take the output of first script into a variable it is taking
everything into a single line. I tried to set as an array but the array
contents are only showing item1.

i have tried google but i couldnt find info on how to set the output of
firstscript ans array content. or can anyone suggest a better idea?

Can anyone please help me out on this.. thanks for your time and
patience for reading a long post .

Icarus Sparry

2005-02-24, 2:58 am

On Wed, 23 Feb 2005 18:35:30 -0800, Professor chaos wrote:

> I have a programm which when executed prints out the following..
>
> item1: somevalue somevalue somevalue somevalue somevalue
> property: value1
> item2: somevalue somevalue somevalue somevalue somevalue
> property: value2


>
> Now i am writing another script which wud call the first script and
> take the following output into and variable say $foo.
>
> for each item i want to know what the value of the property is..
>
> i have to do this using korn shell script. In that i can call awk. but
> i cannot use perl.
>
> My approach is take an array and put all the lines in an array and
> parse for 'item' and if found parse for property in next line and take
> value.


So you want the output to be something like
item1: value1
item2: value2

You can do this in shell

program | while read first rest
do
case "$first" in
property echo "$item" "$rest"
# insert any other processing you want here
;;
*) item="$first" ;;
esac
done


It reads the output of the program, and splits off the first word, and the
rest. If the first word is 'property:' then it uses the rest as the value,
otherwise it uses the first value as the name of the item.

The double semicolons mark the end of a particular case, and esac marks
the end of the case statement as a whole.




Ed Morton

2005-02-24, 7:52 am



Icarus Sparry wrote:
> On Wed, 23 Feb 2005 18:35:30 -0800, Professor chaos wrote:
>
>
>
>
>
>
> So you want the output to be something like
> item1: value1
> item2: value2
>
> You can do this in shell
>
> program | while read first rest
> do
> case "$first" in
> property echo "$item" "$rest"
> # insert any other processing you want here
> ;;
> *) item="$first" ;;
> esac
> done


and here's the equivalent awk:

program | awk '$1 == "property:"{print item,$2; next}{item=$1}'

Ed.

>
> It reads the output of the program, and splits off the first word, and the
> rest. If the first word is 'property:' then it uses the rest as the value,
> otherwise it uses the first value as the name of the item.
>
> The double semicolons mark the end of a particular case, and esac marks
> the end of the case statement as a whole.
>
>
>
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com