awk first word on first line 
Author Message
 awk first word on first line

I am having a problem that I am sure is very easy if you know awk at all..
which I really
don't.

I am trying to test the first word on the first line of a file.  I don't
care about the rest of the file.
I can't figure out how to specify the first line only.  I have a work around
to test the line that contains
the string EDT but I don't want to do that.. I just want to say the first
line regardless of what it contains.

Here is the file

green red red test dkdkd EDT
dkdkd
dkdkd

and the script

#!/bin/sh
host="doreen"
if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
then
    echo $host
fi

How would I change this to say check if green is the first word in the first
line.. not do a search for EDT
to identify the line.

thanks
doreen



Fri, 22 Apr 2005 23:35:35 GMT  
 awk first word on first line

Quote:

> I am having a problem that I am sure is very easy if you know awk at all..
> which I really
> don't.

> I am trying to test the first word on the first line of a file.  I don't
> care about the rest of the file.
> I can't figure out how to specify the first line only.  I have a work
> around to test the line that contains
> the string EDT but I don't want to do that.. I just want to say the first
> line regardless of what it contains.

> Here is the file

> green red red test dkdkd EDT
> dkdkd
> dkdkd

> and the script

> #!/bin/sh
> host="doreen"
> if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
> then
>     echo $host
> fi

> How would I change this to say check if green is the first word in the
> first line.. not do a search for EDT
> to identify the line.

> thanks
> doreen

NR==1 {print $1}

gegroet,
Erik V.



Fri, 22 Apr 2005 23:40:46 GMT  
 awk first word on first line
FNR == 1 {
        print
        # do other stuff
        close(FILENAME)
        }

Actually I suppose the 'FNR==1' is not necessary.

DKM



Quote:
>I am having a problem that I am sure is very easy if you know awk at all..
>which I really
>don't.

>I am trying to test the first word on the first line of a file.  I don't
>care about the rest of the file.
>I can't figure out how to specify the first line only.  I have a work around
>to test the line that contains
>the string EDT but I don't want to do that.. I just want to say the first
>line regardless of what it contains.

>Here is the file

>green red red test dkdkd EDT
>dkdkd
>dkdkd

>and the script

>#!/bin/sh
>host="doreen"
>if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
>then
>    echo $host
>fi

>How would I change this to say check if green is the first word in the first
>line.. not do a search for EDT
>to identify the line.

>thanks
>doreen



Sat, 23 Apr 2005 01:01:50 GMT  
 awk first word on first line


Quote:

> > I am having a problem that I am sure is very easy if you know awk at
all..
> > which I really
> > don't.

> > I am trying to test the first word on the first line of a file.  I don't
> > care about the rest of the file.
> > I can't figure out how to specify the first line only.  I have a work
> > around to test the line that contains
> > the string EDT but I don't want to do that.. I just want to say the
first
> > line regardless of what it contains.

> > Here is the file

> > green red red test dkdkd EDT
> > dkdkd
> > dkdkd

> > and the script

> > #!/bin/sh
> > host="doreen"
> > if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
> > then
> >     echo $host
> > fi

> > How would I change this to say check if green is the first word in the
> > first line.. not do a search for EDT
> > to identify the line.

> > thanks
> > doreen

> NR==1 {print $1}

> gegroet,
> Erik V.

Consider:

    {print $1; exit}

to avoid reading the entire file.

Consider also moving more of your work into AWK, e.g.:

    $1 == "green" {print ENVIRON["host"]}
    {exit}

- Dan



Sat, 23 Apr 2005 01:03:33 GMT  
 awk first word on first line

Quote:
> FNR == 1 {
> print
> # do other stuff
> close(FILENAME)
> }

> Actually I suppose the 'FNR==1' is not necessary.

> DKM



> >I am having a problem that I am sure is very easy if you know awk at
all..
> >which I really
> >don't.

> >I am trying to test the first word on the first line of a file.  I don't
> >care about the rest of the file.
> >I can't figure out how to specify the first line only.  I have a work
around
> >to test the line that contains
> >the string EDT but I don't want to do that.. I just want to say the first
> >line regardless of what it contains.

> >Here is the file

> >green red red test dkdkd EDT
> >dkdkd
> >dkdkd

> >and the script

> >#!/bin/sh
> >host="doreen"
> >if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
> >then
> >    echo $host
> >fi

> >How would I change this to say check if green is the first word in the
first
> >line.. not do a search for EDT
> >to identify the line.

> >thanks
> >doreen

Doug -

The close() is not sufficient to halt processing on some platforms.*  Tested
on gawk 3.1.0,

$ awk '{print; close(FILENAME)}' words
example
chips
time
from
following
spaces
but
quadruple
king

The exit statement does the trick.

* I wonder why not.  I would have hoped it would either skip to the end, or
cause the next meta-loop to reopen the input file, causing a restart from a
file, or a continuation from stdin.

    - Dan



Sat, 23 Apr 2005 01:17:20 GMT  
 awk first word on first line

Quote:



>> FNR == 1 {
>> print
>> # do other stuff
>> close(FILENAME)
>> }

>Doug -

>The close() is not sufficient to halt processing on some platforms.*  Tested
>on gawk 3.1.0,

>$ awk '{print; close(FILENAME)}' words
>example
>chips
>time
>from
>following
>spaces
>but
>quadruple
>king

>The exit statement does the trick.

>* I wonder why not.  I would have hoped it would either skip to the end, or
>cause the next meta-loop to reopen the input file, causing a restart from a
>file, or a continuation from stdin.

>    - Dan

This was discussed here fairly recently.  close() is only for
files/programs opened with redirections, either <, >, or | (or |&
in gawk).  It is a silent error to close something that isn't open in
a redirection, and in recent awks close() is a function.  If you did:

        val = close(FILENAME)
        print val

it should be -1.  ERRNO is even set with a message about it.

At one point I tried to make close(FILENAME) behave like you're thinking, but
that led to problems and I pulled it out.  In any case, it was never documented
in any official place as working that way.

gawk's --lint will warn you:

        $ ./gawk --lint 'BEGIN { val = close("xxx"); print val }'
        gawk: cmd. line:1: warning: close: `xxx' is not an open file, pipe or co-process
        -1

HTH,

Arnold
--

P.O. Box 354            Home Phone: +972  8 979-0381    Fax: +1 928 569 9018
Nof Ayalon              Cell Phone: +972 51  297-545
D.N. Shimshon 99785     ISRAEL



Sat, 23 Apr 2005 16:13:48 GMT  
 awk first word on first line

awk '(NR==1){if(NF>0){first_word_of_first_line=$1;/*processing
first_word_of_first_line...*/;exit;}}'


Quote:
> I am having a problem that I am sure is very easy if you know awk at all..
> which I really
> don't.

> I am trying to test the first word on the first line of a file.  I don't
> care about the rest of the file.
> I can't figure out how to specify the first line only.  I have a work
around
> to test the line that contains
> the string EDT but I don't want to do that.. I just want to say the first
> line regardless of what it contains.

> Here is the file

> green red red test dkdkd EDT
> dkdkd
> dkdkd

> and the script

> #!/bin/sh
> host="doreen"
> if [ `awk '/EDT/{print $1}' /users/doreen/test/${host}.comm` = "green" ]
> then
>     echo $host
> fi

> How would I change this to say check if green is the first word in the
first
> line.. not do a search for EDT
> to identify the line.

> thanks
> doreen



Sun, 24 Apr 2005 00:39:22 GMT  
 awk first word on first line
Hello,

Quote:

> The close() is not sufficient to halt processing on some platforms.*
> Tested on gawk 3.1.0,
...
> * I wonder why not.

Aharon has explained that close() should not interfere with the implicite
loop.

Quote:
> The exit statement does the trick.

Another statement, which would do the trick, would be the `nextfile'
statement.  I think this one is very close to what you required from close().
The only difference is that if there is a next file, it starts a new loop,
so the execution doesn't continue with statements written below `nextfile.'

But note that `nextfile' is not mentioned in POSIX.

Stepan Kasal



Sun, 24 Apr 2005 20:24:03 GMT  
 awk first word on first line
...
Quote:
>Another statement, which would do the trick, would be the `nextfile'
>statement.  I think this one is very close to what you required from
close().
>The only difference is that if there is a next file, it starts a new loop,
>so the execution doesn't continue with statements written below `nextfile.'

...

I wrote Aharon about this a while ago. My idea was a closefile() statement
or function that would allow processing subsequent rules. I don't recall the
reason it wouldn't work.

What could solve the problem would be an EOF (end of file) built-in variable
that was 0 if the next record would be read from the current input source or
1 otherwise (or maybe 1 if the next record would come from the next file
given on the command line and -1 if the current record were the final
record, and END would be processed next). Such a construct would allow
wrapping getline calls in a loop in a user-defined function for
roll-your-own closefile functionality.

That said, if the OP's system has the 'line' command, using that first and
feeding it's output to awk may be the best way to handle the task.



Mon, 25 Apr 2005 04:01:51 GMT  
 awk first word on first line

Quote:

> I am having a problem that I am sure is very easy if you know awk at all..
> which I really
> don't.

> I am trying to test the first word on the first line of a file.  I don't
> care about the rest of the file.
> I can't figure out how to specify the first line only.  I have a work around
> to test the line that contains
> the string EDT but I don't want to do that.. I just want to say the first
> line regardless of what it contains.

head -n 1 yourfile |cut -d " " -f 1

:O)
Pavel Hampl



Mon, 25 Apr 2005 21:43:45 GMT  
 awk first word on first line

Quote:


>> I am having a problem that I am sure is very easy if you know awk at all..
>> which I really
>> don't.

>> I am trying to test the first word on the first line of a file.  I don't
>> care about the rest of the file.
>> I can't figure out how to specify the first line only.  I have a work around
>> to test the line that contains
>> the string EDT but I don't want to do that.. I just want to say the first
>> line regardless of what it contains.

> head -n 1 yourfile |cut -d " " -f 1

>:O)

     Why use any external commands?

        read word junk < yourfile
        echo $word

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



Tue, 26 Apr 2005 09:18:18 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. AWK question: first line that matches?

2. AWK: Problem processing first line

3. How to replace one or two words with one word with one line of awk code

4. Selecting first line on screen and current line after reread in browselist

5. text widget, how to get line number of first line displayed

6. first post, first UCBlogo & MW2 thoughts

7. first setup, first login?

8. breadth-first vs depth-first...

9. pythonic way of depth first and breadth first tree iterators

10. upcase/downcase first character of a word

11. First X Words in a String can't use [string wordend] nor [lrange]

12. My first awk program

 

 
Powered by phpBB® Forum Software