processing one line in a file at a time using awk 
Author Message
 processing one line in a file at a time using awk

Hello,

I need to read a line in a file (it's a number/string), get the
modulus of the number, create the appropriate print statement based on
the result, then read the next number in the file, etc. until EOF.

Essentially, it is this:

records.out
123456789
1122334455
22334455
344442222

read first number in records.out # 123456789
get the modulus # after some manipulation, divide the number by four
use the result in the print statement
read the next number

I tried the following just for reading the first number and saving it
(I have to start somewhere), but got errors:

awk 'BEGIN { while (getline < "records.out" > 0) } {
   printf "%s", $1 } records.out >> __temp_record.input'
set SVC = `awk '{printf "%s", $1}' __temp_record.input`
set NUM_LENGTH = `awk '(length ($1) - 8)' __temp_record.input`

I'm lost.  Please help.

Thank you,
Diane



Sun, 25 Jan 2004 01:51:20 GMT  
 processing one line in a file at a time using awk

Quote:

> I need to read a line in a file (it's a number/string), get the
> modulus of the number, create the appropriate print statement based on
> the result, then read the next number in the file, etc. until EOF.

> Essentially, it is this:

> records.out
> 123456789
> 1122334455
> 22334455
> 344442222

> read first number in records.out # 123456789
> get the modulus # after some manipulation, divide the number by four
> use the result in the print statement
> read the next number

awk '
{
  x = $1
  # get the modulus (of/by what?) and do your manipulations
  print x
Quote:
}

' records.out

And perhaps you have a look at
<http://www.oreilly.com/catalog/awkprog3/>, it's a very good book.

Regards...
                Michael



Sun, 25 Jan 2004 02:16:38 GMT  
 processing one line in a file at a time using awk

[...]

% I tried the following just for reading the first number and saving it
% (I have to start somewhere), but got errors:
%
% awk 'BEGIN { while (getline < "records.out" > 0) } {
%    printf "%s", $1 } records.out >> __temp_record.input'

You get errors because you don't have a body to your while loop.
You're also mixing input methods -- you can retrieve data from a
file using getline, or you can put the file name on the command-line
and use awk's automatic input loop, but there's no need to do both,
so:
  awk 'BEGIN { while (getline < records.out" > 0) {
                  printf "%s", $1
               }
  }' >> __temp_record.input

or

  awk '{ printf "%s", $1 }' records.out >> __temp_record.input

[the other thing you're doing is putting the input file name and
output redirection within the single-quotes, making them part
of the awk program, and so subject to syntax errors, and hiding
them from the shell].

% set SVC = `awk '{printf "%s", $1}' __temp_record.input`
% set NUM_LENGTH = `awk '(length ($1) - 8)' __temp_record.input`

These look OK, I guess (assuming c-shell), although they will
probably have more values than you're expecting. I suggest
avoiding shell-awk interaction where possible. It seems like
to do your bit, you  want something like:
  awk '{ modn = $1 % 10 # you don't say modulo what
         if (modn == 0) printf "mod 0 %s\n", $1
         else if (modn == 1) printf "%s is mod 1\n", $1
         # ...
  }' records.out > records.in

depending on exactly what needs to be done with printf, you
could do something clever like stick all the printf statements
in an array in a BEGIN section, and just pull them out as needed:
 awk 'BEGIN { ps[0] = "printf statement 0 %s"
              ps[1] = "1st printf statement for %s"; ... }
      { printf ps[$1 % 10], $1 }' records.out > records.in

Hope this helps.
--

Patrick TJ McPhee
East York  Canada



Sun, 25 Jan 2004 10:54:06 GMT  
 processing one line in a file at a time using awk

Quote:


> [...]
>   awk '{ modn = $1 % 10 # you don't say modulo what

Yes, C shell.  Thank you for your suggestions -- I will try them.

The algorithm I need to perform on each number in records.out is:

i = length of number - 8
if (i < 0), i = 0
x = (number + i) modulo 4

I like the idea of using an array for the print statements, but how do
I apply the above algorithm?

Diane



Sun, 25 Jan 2004 22:27:05 GMT  
 processing one line in a file at a time using awk

% The algorithm I need to perform on each number in records.out is:
%
% i = length of number - 8

  # The number is $1, which we can call n:
  {
      n = $1
      # we can call length of n ln
      ln = length(n)

      # therefore
      i = ln - 8

% if (i < 0), i = 0

      if (i < 0) i = 0

% x = (number + i) modulo 4

      # % is not strictly a modulo operator, since the sign is always
      # the same as the numerator. Fortunately, we've forced this to
      # be positive (provided n > 0)
      x = (n + i) % 4

   }

% I like the idea of using an array for the print statements, but how do
% I apply the above algorithm?

Now, it depends on what goes in the print statements, but you could
have

   BEGIN {
    ps[0] = "n is %d in print statement 0\n"
    ps[1] = "n is %d in print statement 1\n"
    ps[2] = "n is %d in print statement 2\n"
    ps[3] = "n is %d in print statement 3\n"
   }
   { printf ps[x], n }

provided that all the print statements take the same arguments, in the
same order.
--

Patrick TJ McPhee
East York  Canada



Tue, 27 Jan 2004 10:55:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

2. Help on using AWK to process an XML file

3. reading files one line at a time.

4. Outputting info from awk and using the info to name the awk output file

5. awk/gawk stop after processing 40mill lines

6. AWK: Problem processing first line

7. how to compile .vhd files one by one using makefile

8. Executing several processes one at a time.

9. problems running more than one process a time

10. Executing Fortran Code One Line at a Time

11. process only last record using awk

12. searching one file using the contents of another file

 

 
Powered by phpBB® Forum Software