question on awk

question on awk

am 04.01.2008 17:39:15 von zmasood

Hi All,

I want to be able to read the contents of a file as below:-

==================================
There are 4 occurrences

These occurrences are listed below:
machine1---RUNNING
machine2---STOPPED
machine3---WORKING
machine4---RUNNING

==================================

I want the script to pick all the machines in RUNNING state i.e.
machines1 and 4 and assign their names to a variable for further
manipulation.

Do I use awk?

Please help

TIA

Re: question on awk (FU2 set, comp.lang.awk posted)

am 04.01.2008 17:56:18 von Loki Harfagr

On Fri, 04 Jan 2008 08:39:15 -0800, Zinger wrote:

> Hi All,
>
> I want to be able to read the contents of a file as below:-
>
> ==================================
> There are 4 occurrences
>
> These occurrences are listed below:
> machine1---RUNNING
> machine2---STOPPED
> machine3---WORKING
> machine4---RUNNING
>
> ==================================
>
> I want the script to pick all the machines in RUNNING state i.e.
> machines1 and 4 and assign their names to a variable for further
> manipulation.
>
> Do I use awk?
>
> Please help
>
> TIA

Well, the real answer depends much on the rest of what you
expect to do overall, but just supposing you really want to
fill a variable with previous problem description, it's
possible to do for instance:

$ woooop=$(awk '$NF=="RUNNING"{print $1}' FS=- yourfile)

or backticks instead of bashism:
$ woooop=`awk '$NF=="RUNNING"{print $1}' FS=- yourfile`


then use your variable at your will:
$ echo ${woooop}
machine1 machine4

$ echo "${woooop}"
machine1
machine4

Re: question on awk

am 04.01.2008 18:12:03 von Ed Morton

On 1/4/2008 10:39 AM, Zinger wrote:
> Hi All,
>
> I want to be able to read the contents of a file as below:-
>
> ==================================
> There are 4 occurrences
>
> These occurrences are listed below:
> machine1---RUNNING
> machine2---STOPPED
> machine3---WORKING
> machine4---RUNNING
>
> ==================================
>
> I want the script to pick all the machines in RUNNING state i.e.
> machines1 and 4 and assign their names to a variable for further
> manipulation.
>
> Do I use awk?

Please don't multi-post as this was already answered in comp.lang.awk under the
assumption this was strictly an awk question:

Yes:

awk -F- '$NF=="RUNNING"{print $1}' file

To assign each name to a variable as you go would be:

awk -F- '$NF=="RUNNING"{var = $1}' file

but that obviously wouldn't produce any output and may not be the best approach.
You'd have to tell us what the "further manipulation" is for more help with that.

When you say "assign their names to a variable" do you mean an awk variable or a
shell variable? If you're looking to just write an awk script, post followups to
comp.lang.awk. If you're looking to embed awk in a longer shell script or aren't
sure how to proceed, post followups to comp.unix.shell.

Ed.

Re: question on awk

am 04.01.2008 18:19:04 von Cyrus Kriticos

Zinger wrote:
>
> I want to be able to read the contents of a file as below:-
>
> ==================================
> There are 4 occurrences
>
> These occurrences are listed below:
> machine1---RUNNING
> machine2---STOPPED
> machine3---WORKING
> machine4---RUNNING
>
> ==================================
>
> I want the script to pick all the machines in RUNNING state i.e.
> machines1 and 4 and assign their names to a variable for further
> manipulation.

export $(sed -n "/RUNNING/{s/---/=/;s/$/ /p}" filename)

> Do I use awk?

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: question on awk

am 04.01.2008 18:23:17 von zmasood

On Jan 4, 12:12 pm, Ed Morton wrote:
> On 1/4/2008 10:39 AM, Zinger wrote:
>
>
>
> > Hi All,
>
> > I want to be able to read the contents of a file as below:-
>
> > ==================================
> > There are 4 occurrences
>
> > These occurrences are listed below:
> > machine1---RUNNING
> > machine2---STOPPED
> > machine3---WORKING
> > machine4---RUNNING
>
> > ==================================
>
> > I want the script to pick all the machines in RUNNING state i.e.
> > machines1 and 4 and assign their names to a variable for further
> > manipulation.
>
> > Do I use awk?
>
> Please don't multi-post as this was already answered in comp.lang.awk under the
> assumption this was strictly an awk question:
>
> Yes:
>
> awk -F- '$NF=="RUNNING"{print $1}' file
>
> To assign each name to a variable as you go would be:
>
> awk -F- '$NF=="RUNNING"{var = $1}' file
>
> but that obviously wouldn't produce any output and may not be the best approach.
> You'd have to tell us what the "further manipulation" is for more help with that.
>
> When you say "assign their names to a variable" do you mean an awk variable or a
> shell variable? If you're looking to just write an awk script, post followups to
> comp.lang.awk. If you're looking to embed awk in a longer shell script or aren't
> sure how to proceed, post followups to comp.unix.shell.
>
> Ed.

Thanks a lot!

Sorry for the multi-post as I did not see my questions appearing in
the list the first time.

By saying "assign their names to a variable" I meant a shell variable
as I want to take the names of "machine1" and "machine4" in this case
and assign them as an argument to a different script for stopping them
individually.

Thanks again

Re: question on awk

am 04.01.2008 18:27:05 von Ed Morton

On 1/4/2008 11:23 AM, Zinger wrote:
> On Jan 4, 12:12 pm, Ed Morton wrote:
>
>>On 1/4/2008 10:39 AM, Zinger wrote:
>>
>>
>>
>>
>>>Hi All,
>>
>>>I want to be able to read the contents of a file as below:-
>>
>>>==================================
>>>There are 4 occurrences
>>
>>>These occurrences are listed below:
>>>machine1---RUNNING
>>>machine2---STOPPED
>>>machine3---WORKING
>>>machine4---RUNNING
>>
>>>==================================
>>
>>>I want the script to pick all the machines in RUNNING state i.e.
>>>machines1 and 4 and assign their names to a variable for further
>>>manipulation.
>>
>>>Do I use awk?
>>
>>Please don't multi-post as this was already answered in comp.lang.awk under the
>>assumption this was strictly an awk question:
>>
>>Yes:
>>
>> awk -F- '$NF=="RUNNING"{print $1}' file
>>
>>To assign each name to a variable as you go would be:
>>
>> awk -F- '$NF=="RUNNING"{var = $1}' file
>>
>>but that obviously wouldn't produce any output and may not be the best approach.
>>You'd have to tell us what the "further manipulation" is for more help with that.
>>
>>When you say "assign their names to a variable" do you mean an awk variable or a
>>shell variable? If you're looking to just write an awk script, post followups to
>>comp.lang.awk. If you're looking to embed awk in a longer shell script or aren't
>>sure how to proceed, post followups to comp.unix.shell.
>>
>> Ed.
>
>
> Thanks a lot!
>
> Sorry for the multi-post as I did not see my questions appearing in
> the list the first time.
>
> By saying "assign their names to a variable" I meant a shell variable
> as I want to take the names of "machine1" and "machine4" in this case
> and assign them as an argument to a different script for stopping them
> individually.
>
> Thanks again
>

SOmething like this:

awk -F- '$NF=="RUNNING"{print $1}' file |
while IFS= read -r machine
do
different_script "$machine"
done

might be what you're looking for then.

Ed.

Re: question on awk

am 04.01.2008 19:19:11 von mik3l3374

On Jan 5, 12:39 am, Zinger wrote:
> Hi All,
>
> I want to be able to read the contents of a file as below:-
>
> ==================================
> There are 4 occurrences
>
> These occurrences are listed below:
> machine1---RUNNING
> machine2---STOPPED
> machine3---WORKING
> machine4---RUNNING
>
> ==================================
>
> I want the script to pick all the machines in RUNNING state i.e.
> machines1 and 4 and assign their names to a variable for further
> manipulation.
>
> Do I use awk?
>
> Please help
>
> TIA

while read line
do
case $line in
*RUNNING)
var=$line #if you want to assign to another variable
echo "Further processing on $var ..."
esac
done < "file"

Re: question on awk

am 04.01.2008 19:30:45 von zmasood

On Jan 4, 12:27 pm, Ed Morton wrote:
> On 1/4/2008 11:23 AM, Zinger wrote:
>
>
>
> > On Jan 4, 12:12 pm, Ed Morton wrote:
>
> >>On 1/4/2008 10:39 AM, Zinger wrote:
>
> >>>Hi All,
>
> >>>I want to be able to read the contents of a file as below:-
>
> >>>==================================
> >>>There are 4 occurrences
>
> >>>These occurrences are listed below:
> >>>machine1---RUNNING
> >>>machine2---STOPPED
> >>>machine3---WORKING
> >>>machine4---RUNNING
>
> >>>==================================
>
> >>>I want the script to pick all the machines in RUNNING state i.e.
> >>>machines1 and 4 and assign their names to a variable for further
> >>>manipulation.
>
> >>>Do I use awk?
>
> >>Please don't multi-post as this was already answered in comp.lang.awk under the
> >>assumption this was strictly an awk question:
>
> >>Yes:
>
> >> awk -F- '$NF=="RUNNING"{print $1}' file
>
> >>To assign each name to a variable as you go would be:
>
> >> awk -F- '$NF=="RUNNING"{var = $1}' file
>
> >>but that obviously wouldn't produce any output and may not be the best approach.
> >>You'd have to tell us what the "further manipulation" is for more help with that.
>
> >>When you say "assign their names to a variable" do you mean an awk variable or a
> >>shell variable? If you're looking to just write an awk script, post followups to
> >>comp.lang.awk. If you're looking to embed awk in a longer shell script or aren't
> >>sure how to proceed, post followups to comp.unix.shell.
>
> >> Ed.
>
> > Thanks a lot!
>
> > Sorry for the multi-post as I did not see my questions appearing in
> > the list the first time.
>
> > By saying "assign their names to a variable" I meant a shell variable
> > as I want to take the names of "machine1" and "machine4" in this case
> > and assign them as an argument to a different script for stopping them
> > individually.
>
> > Thanks again
>
> SOmething like this:
>
> awk -F- '$NF=="RUNNING"{print $1}' file |
> while IFS= read -r machine
> do
> different_script "$machine"
> done
>
> might be what you're looking for then.
>
> Ed.

Thanks Ed! You are the man!

Regards

Re: question on awk

am 04.01.2008 21:30:35 von Ed Morton

On 1/4/2008 12:30 PM, Zinger wrote:
> On Jan 4, 12:27 pm, Ed Morton wrote:
>
>>On 1/4/2008 11:23 AM, Zinger wrote:
>>
>>
>>
>>
>>>On Jan 4, 12:12 pm, Ed Morton wrote:
>>
>>>>On 1/4/2008 10:39 AM, Zinger wrote:
>>>
>>>>>Hi All,
>>>>
>>>>>I want to be able to read the contents of a file as below:-
>>>>
>>>>>==================================
>>>>>There are 4 occurrences
>>>>
>>>>>These occurrences are listed below:
>>>>>machine1---RUNNING
>>>>>machine2---STOPPED
>>>>>machine3---WORKING
>>>>>machine4---RUNNING
>>>>
>>>>>==================================
>>>>
>>>>>I want the script to pick all the machines in RUNNING state i.e.
>>>>>machines1 and 4 and assign their names to a variable for further
>>>>>manipulation.
>>>>
>>>>>Do I use awk?
>>>>
>>>>Please don't multi-post as this was already answered in comp.lang.awk under the
>>>>assumption this was strictly an awk question:
>>>
>>>>Yes:
>>>
>>>> awk -F- '$NF=="RUNNING"{print $1}' file
>>>
>>>>To assign each name to a variable as you go would be:
>>>
>>>> awk -F- '$NF=="RUNNING"{var = $1}' file
>>>
>>>>but that obviously wouldn't produce any output and may not be the best approach.
>>>>You'd have to tell us what the "further manipulation" is for more help with that.
>>>
>>>>When you say "assign their names to a variable" do you mean an awk variable or a
>>>>shell variable? If you're looking to just write an awk script, post followups to
>>>>comp.lang.awk. If you're looking to embed awk in a longer shell script or aren't
>>>>sure how to proceed, post followups to comp.unix.shell.
>>>
>>>> Ed.
>>>
>>>Thanks a lot!
>>
>>>Sorry for the multi-post as I did not see my questions appearing in
>>>the list the first time.
>>
>>>By saying "assign their names to a variable" I meant a shell variable
>>>as I want to take the names of "machine1" and "machine4" in this case
>>>and assign them as an argument to a different script for stopping them
>>>individually.
>>
>>>Thanks again
>>
>>SOmething like this:
>>
>>awk -F- '$NF=="RUNNING"{print $1}' file |
>>while IFS= read -r machine
>>do
>> different_script "$machine"
>>done
>>
>>might be what you're looking for then.
>>
>> Ed.
>
>
> Thanks Ed! You are the man!
>

Of course, you don't actually need awk for this since you're using a shell loop
anyway:

while IFS=- read -r machine x y status; do
if [ "$status" = "RUNNING" ]; then
different_script "$machine"
fi
done < file

Regards,

Ed.

Re: question on awk

am 04.01.2008 21:55:40 von Ed Morton

On 1/4/2008 2:30 PM, Ed Morton wrote:
>
> On 1/4/2008 12:30 PM, Zinger wrote:
>
>>On Jan 4, 12:27 pm, Ed Morton wrote:
>>
>>
>>>On 1/4/2008 11:23 AM, Zinger wrote:
>>>
>>>
>>>
>>>
>>>
>>>>On Jan 4, 12:12 pm, Ed Morton wrote:
>>>
>>>>>On 1/4/2008 10:39 AM, Zinger wrote:
>>>>
>>>>>>Hi All,
>>>>>
>>>>>>I want to be able to read the contents of a file as below:-
>>>>>
>>>>>>==================================
>>>>>>There are 4 occurrences
>>>>>
>>>>>>These occurrences are listed below:
>>>>>>machine1---RUNNING
>>>>>>machine2---STOPPED
>>>>>>machine3---WORKING
>>>>>>machine4---RUNNING
>>>>>
>>>>>>==================================
>>>>>
>>>>>>I want the script to pick all the machines in RUNNING state i.e.
>>>>>>machines1 and 4 and assign their names to a variable for further
>>>>>>manipulation.
>>>>>
>>>>>>Do I use awk?

>>>>By saying "assign their names to a variable" I meant a shell variable
>>>>as I want to take the names of "machine1" and "machine4" in this case
>>>>and assign them as an argument to a different script for stopping them
>>>>individually.
>>>
>>>>Thanks again
>>>
>>>SOmething like this:
>>>
>>>awk -F- '$NF=="RUNNING"{print $1}' file |
>>>while IFS= read -r machine
>>>do
>>> different_script "$machine"
>>>done
>>>
>>>might be what you're looking for then.
>>>
>>> Ed.
>>
>>
>>Thanks Ed! You are the man!
>>
>
>
> Of course, you don't actually need awk for this since you're using a shell loop
> anyway:
>
> while IFS=- read -r machine x y status; do
> if [ "$status" = "RUNNING" ]; then
> different_script "$machine"
> fi
> done < file

Slightly preferable syntax (IMHO):

while IFS=- read -r machine x y status; do
[ "$status" = "RUNNING" ] && different_script "$machine"
done < file

I probably just prefer it as it looks more awkish :-).

Ed.