Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transform contents of text file 1

Status
Not open for further replies.

zanzemaj

Programmer
Nov 2, 2006
35
NZ
Hi,
I need help in transforming a text file. Here is an example of file1.txt:

Code:
pos1 TABLE ah_feed 3828 48
pos1 TABLE ah_feed 7402 103
po1 VWW ah_feed 1234 123
po1 VWW ah_feed 1234 123
pos2 TABLE DEPT_XREF 6365942 181866
pos2 TABLE DEPT_XREF 12758867 364521
pos2 VWW dep_f 122 2
pos2 TABLE dept_xref_bkup 123 11
pos2 TABLE dept_xref_bkup 128 12
pos2 TABLE dfso_pos_tr 12694 24
pos2 TABLE dfso_pos_tr 20278 40
pos2 TABLE div_rpt 504 0
pos2 TABLE div_rpt 553 1
pos2 MCR pos_f 123 0

Please help me check if the 1st, 2nd and 3rd argument is the same, delete the line where the 4th and 5th argument is lower, also if the 4th and 5th argument is same or equal, delete one line. file1.txt should look like this:

Code:
pos1 TABLE ah_feed 7402 103
po1 VWW ah_feed 1234 123
pos2 TABLE DEPT_XREF 12758867 364521
pos2 VWW dep_f 122 2
pos2 TABLE dept_xref_bkup 128 12
pos2 TABLE dfso_pos_tr 20278 40
pos2 TABLE div_rpt 553 1
pos2 MCR pos_f 123 0

I only got this code as a start:
awk '/TABLE|VWW|MCR/ {print ; next; }' file1.txt > file2.txt

But I do not know how to proceed and go about comparing the values. Thank you in advance for you help.
 
I don't know how to do it in awk, but it's relatively straightforward in perl
Code:
#!/usr/bin/perl -w
use strict;

my %op; # hash to store the values
        # the format will be a key consisting of the first three words
        # storing a reference to an array of the two numeric values
while (<>) #for each line of the file
  {
  my ( $key, $val1, $val2 ); 
  # split the file into key and two values
  /^(\w+\s+\w+\s+\w+)\s+(\d+)\s+(\d+)/ and $key = $1, $val1 = $2, $val2 = $3;
  # if this is the first with this key then store and next;
  exists $op{$key} or $op{$key} = [ $val1, $val2 ], next;
  # if existing key has bigger or equal value then next;
  ( $op{$key}->[0] >= $val1 ) and next;
  # store the new value
  $op{$key} = [ $val1, $val2 ];
  }

# print out the results
foreach ( keys %op )
  { print "$_ $op{$_}->[0] $op{$_}->[1]\n"; }


Ceci n'est pas une signature
Columb Healy
 
Thank you Columb, I really appreciate your help.

However, can anyone suggest something I could use in ksh? Thank you in advance for your replies.
 
nawk -f zan.awk text1.txt

zan.awk:
Code:
{
  idx= $1 OFS $2 OFS $3

  if ( !(idx in arr))
    arr[idx] = $4 OFS $5
  else {
    split(arr[idx], tmpA, OFS)
    if (tmpA[1] < $4 && tmp[2] < $5)
     arr[idx] = $4 OFS $5
  }
}
END {
  for(i in arr)
    print i OFS arr[i]
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thank you vgersh99, it works as expected. Great help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top