getline problem 
Author Message
 getline problem

I am having problems reading and parsing lines of text from an input
file.

When I run the following AWK script:

        awk 'BEGIN {
                 while ((getline < "Infile.asc") > 0)
                 print $1
                }'

I get a listing of the first field in every line of the input file.
That is what I expected.

When I run the same basic script but add lines to parse and print a
substring

        awk 'BEGIN {
                while ((getline < "Infile.asc") > 0)
                  FIRSTCHARS = substr($0,1,10)
                  print FIRSTCHARS
        }'

I only get a the first 10 characters of the last line of the input
file.

What I need is the first 10 characters of every line.

Would someone please enlighten me.

Thanks



Sun, 07 Dec 2003 01:36:09 GMT  
 getline problem

Quote:

>        awk 'BEGIN {
>                while ((getline < "Infile.asc") > 0)
                 {
>                  FIRSTCHARS = substr($0,1,10)
>                  print FIRSTCHARS
                 }
>        }'

> I only get a the first 10 characters of the last line of the input
> file.

Regards...
                Michael


Sun, 07 Dec 2003 02:29:50 GMT  
 getline problem

Quote:

> I am having problems reading and parsing lines of text from an input
> file.

> When I run the following AWK script:

>    awk 'BEGIN {
>             while ((getline < "Infile.asc") > 0)
>             print $1
>            }'

> I get a listing of the first field in every line of the input file.
> That is what I expected.

> When I run the same basic script but add lines to parse and print a
> substring

>    awk 'BEGIN {
>            while ((getline < "Infile.asc") > 0)
>              FIRSTCHARS = substr($0,1,10)
>              print FIRSTCHARS
>    }'

> I only get a the first 10 characters of the last line of the input
> file.

> What I need is the first 10 characters of every line.

You have a bug: missing braces. You need to enclose a block consisting
of two or more statements in curly braces.

    awk 'BEGIN {
        while ((getline <"Infile.asc") > 0) {
            FIRSTCHARS = substr($0, 1, 10)
            print FIRSTCHARS
        }
    }'

Is this the *whole* script? It reduces to

    awk '{ print substr($0, 1, 10) }' Infile.asc

which reduces to

    cut -c 1-10 Infile.asc

--
Jim Monty

Tempe, Arizona USA



Sun, 07 Dec 2003 03:40:29 GMT  
 getline problem

Quote:
>    awk 'BEGIN {
>            while ((getline < "Infile.asc") > 0)
>              FIRSTCHARS = substr($0,1,10)
>              print FIRSTCHARS
>    }'

>I only get a the first 10 characters of the last line of the input
>file.

You may have noticed this by now, but the block of code
following your while loop isn't incased in '{' '}' brackets.
FIRSTCHARS is being computed everytime, but only
printed once.

Jacob

Jacob

Note: To reply to my email,
remove the .nospam from the address.



Tue, 09 Dec 2003 11:30:06 GMT  
 getline problem

Quote:
>    awk 'BEGIN {
>            while ((getline < "Infile.asc") > 0)
>              FIRSTCHARS = substr($0,1,10)
>              print FIRSTCHARS
>    }'

>I only get a the first 10 characters of the last line of the input
>file.

You may have noticed this by now, but the block of code
following your while loop isn't incased in '{' '}' brackets.
FIRSTCHARS is being computed everytime, but only
printed once.

Jacob

Note: To reply to my email,
remove the .nospam from the address.



Tue, 09 Dec 2003 11:31:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. newbie help - getline problem

2. Another Getline problem

3. getline problems

4. Getline problems with Lookup file

5. getline problem : solved

6. getline problem

7. awk lookup problem using getline

8. problem with multiple getline

9. problem with multiple getline

10. quoting problem with piping command to getline

11. Problems with getline in cygwin awk

12. Problems with various forms of getline

 

 
Powered by phpBB® Forum Software