For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2008 > Newbie question on awk









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 Newbie question on awk
Zinger

2008-01-04, 6:58 pm

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


loki harfagr

2008-01-04, 6:58 pm

On Fri, 04 Jan 2008 08:24:46 -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?


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


Ed Morton

2008-01-04, 6:58 pm



On 1/4/2008 10:24 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?


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.

Ed.

Ted Davis

2008-01-04, 9:58 pm

On Fri, 04 Jan 2008 08:24:46 -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?


I might, or might not, depending on the operating system and what I wanted
to do with the list. It is also unclear whether you want all the names in
a single string, one at a time in a variable, or in a table.

What are you trying to do? in what operating system?


--

T.E.D. (tdavis@umr.edu) UMR becomes MST soon.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com