awk algorithms

Discuss Programming

awk algorithms

Postby worker201 » Thu Dec 09, 2004 5:51 pm

I'm trying to design an awk program/statement that will streamline file editing.

Here's the input file structure (it will always have exactly 9 lines and exactly 8 records per line):
x1 x2 x3 x4 x5 x6 x7 x8
x9 xN etcetera

These are just numbers, auto-generated by another program.

I want to rewrite some of them with specific values:
x1 0 105 166 x5 0 116 156
x9 0 etcetera

(basically, I am changing the values for RGB triples - and the new colors are constants)

I tried doing something like:
Code: Select all
print $1,0,105,166,$5,0,116,156

but this prints the same RGB values for every line, with only the first and 5th records changing.

I'm thinking that maybe getline is the way to go with this, but I don't quite understand how to use it.

Please help?

EDIT: actually, it doesn't have to be in awk. Whatever works is going into a larger bash script. Sadly, I think awk is my strongest programming language... :oops:
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Thu Dec 09, 2004 6:06 pm

So you are talking 9 lines of 8 columns. I am a little confused as to your requirements. Could you provide a sample file, along with what you want it to look like after it has been processed? This should be a very easy task.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby worker201 » Thu Dec 09, 2004 6:28 pm

Okay, an example input file:
http://www.triple-bypass.net/download/sample_in.txt

output file:
http://www.triple-bypass.net/download/sample_out.txt

As you can see, I wish to have the 1st and 5th column of numbers remain the same, and change the 2nd, 3rd, 4th, 6th, 7th and 8th columns to constant values.

They have to be separated into records, so that they can be read by another program - hence, my disposition toward awk.

Thanks for your help.

Incidentally, I would like to copy the first 3 lines and last 3 lines exactly, but I already know how to do that.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Thu Dec 09, 2004 6:59 pm

You say you want the 1st and 5th columns to stay the same but the 1st and 5th columns in your first example file are not the same as the 1st and 5th columns in your second example. I am still confused. And when you say you want the rest of the columns to change to constant values does that mean for instance that you want the 2nd column to always have the values 0,0,0,0,0,17,75,144,205?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby worker201 » Thu Dec 09, 2004 7:21 pm

Sorry about that typo

And when you say you want the rest of the columns to change to constant values does that mean for instance that you want the 2nd column to always have the values 0,0,0,0,0,17,75,144,205?


Yes. The input may not have those values for the 2nd column, but I want the output to have those values. Similarly for 3, 4, 6, 7, and 8.

So.
The first and fifth colums represent ranges of ocean depths to be colored. Having two different colors (rgb triples) will cause the program to make a gradient. The depth ranges and default color values are output by program A. I want to keep the depth ranges, but customize the color values. I have a set of color values I want to use, which is the set noted in the output example. This file then gets sent to program B. In the past, I have modified the file by "hand" (in gedit or vi), but that is so tedious. So I wish to automate the process.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Thu Dec 09, 2004 8:31 pm

Well, it's maybe not the shortest way to do it but it's the first way that popped into my head:

rgbcvt.pl:
Code: Select all
#!/usr/bin/perl -w
my @g = ([  0,  0,105,166,  0,  0,116,156],
         [  0,  0,116,156,  0,  0,132,141],
         [  0,  0,132,141,  0,  0,150,125],
         [  0,  0,150,125,  0,  0,167,112],
         [  0,  0,167,112,  0, 17,184,103],
         [  0, 17,184,103,  0, 75,202, 99],
         [  0, 75,202, 99,  0,144,219, 97],
         [  0,144,219, 97,  0,205,232, 97],
         [  0,205,232, 97,  0,255,244, 96]);

$x=0;
while (<STDIN>) {
   if (/^\#/ || $x >= 9) { print; next; }
   @r = split("\t");
   $g[$x][0]=$r[0];
   $g[$x][4]=$r[4];
   for ($y=0;$y<8;$y++) {
     print "$g[$x][$y]";
     if ($y < 7) { print "\t"; } else { print "\n"; }
   }
   $x++;
}


cat infile.txt | ./rgbcvt.pl > outfile.txt
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby worker201 » Fri Dec 10, 2004 1:08 am

Cool, perl. I know almost nothing of Perl. But I am going to go ahead and assume you meant to start the file with '#!' and not '#1'.

The only expression that really escapes me (besides the '-w' on the first line) is this one:
Code: Select all
if (/^\#/ || $x >= 9)

Can you sorta explain what that means? I think I can sort the rest out on my own.

Of course, if you really want to, you could summarize each expression in English, but it's not required.:D

I appreciate the help. While this will work for the time being, I eventually would like to construct the program myself, since copying a perl script doesn't help me learn anything. But thanks again.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Fri Dec 10, 2004 8:39 am

worker201 wrote:Cool, perl. I know almost nothing of Perl. But I am going to go ahead and assume you meant to start the file with '#!' and not '#1'.

The only expression that really escapes me (besides the '-w' on the first line) is this one:
Code: Select all
if (/^\#/ || $x >= 9)

Can you sorta explain what that means? I think I can sort the rest out on my own.

Of course, if you really want to, you could summarize each expression in English, but it's not required.:D

I appreciate the help. While this will work for the time being, I eventually would like to construct the program myself, since copying a perl script doesn't help me learn anything. But thanks again.


I did start it out with a "#!". You need to copy/paste the text into an editor. If you get a "1" then something is broken. The "-w" switch causes Perl to spit out a little more diagnostic info should something go wrong with your code, or you have syntax errors that might otherwise have slipped by unnoticed. The "/^#/" means if the line starts with a "#" and of course "$x >= 9" means if the value of the variable $x is greater than or equal to 9. The "||" means "or". So if either of those two conditions are true I just want to output the lineand get the next one without any further processing of it.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests