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!

combine output 1

Status
Not open for further replies.

Dorga

MIS
Jul 30, 2003
101
NZ
I have many files named as such:

04-06-2004.out
04-07-2004.out
etc..

The files have the following data..

04/06/2004
0 2797
1 17
150 1277
196 436
239 34
41 14
54 4
58 2
59 2
84 22

04/07/2004
0 5389
1 6
150 372
196 32
239 56
28 2
49 1
52 12
54 6
58 21
59 4
80 1
84 28
96 126
98 1

The first line always starts with the date, the number on the left must stay relative to the number on the right, the left is an error code, the number on the right is the times that error code happened.

What I need to do is, take each day's files and make them like this.

Code 04/06/2004 04/07/2004
0 2797 5389
1 1277 6
28 0 2
41 14 0
49 0 1
52 0 12
54 4 6
58 2 21
59 2 4
80 0 1
84 22 28
96 0 126
98 0 1
150 1277 372
196 436 32
239 34 56

Thanks for any help...
 
something like this should get you going - assuming all the files to be processed have '.out' extention:


nawk -f dorga.awk *.out

Code:
FNR==1 { date = $0; dates = (!dates) ? date : dates SUBSEP date; next}

{
  codes[$1];
  codDat[$1,date] = $2
}

END {

  nDates=split(dates, datesA, SUBSEP);
  printf("%-10s", "Code");
  for (i=1; i <= nDates; i++)
      printf("%-15s%s", datesA[i], ( i == nDates) ? "\n" : "" );

  for ( c in codes) {
     printf("%-10d", c);
     for (cd in codDat) {
        split(cd, cdA, SUBSEP);
        if ( cdA[1] != c ) continue;
        for (j=1; j <= nDates; j++) {
           idx=c SUBSEP datesA[j];
           printf("%-15s", (idx in codDat) ? codDat[idx] : 0);
           delete codDat[idx];
        }
        printf("\n");
     }
  }
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
sorry for the previous post - a bit better this time:

Code:
FNR==1 { date = $0; dates = (!dates) ? date : dates SUBSEP date; next}

{
  codes[$1];
  codDat[$1,date] = $2
}

END {

  nDates=split(dates, datesA, SUBSEP);
  printf("%-10s", "Code");
  for (i=1; i <= nDates; i++)
      printf("%15s%s", datesA[i], ( i == nDates) ? "\n" : "" );

  for ( c in codes) {
     printf("%-10d", c);
     for (j=1; j <= nDates; j++) {
        idx=c SUBSEP datesA[j];
        printf("%15s", (idx in codDat) ? codDat[idx] : 0);
        delete codDat[idx];
     }
     printf("\n");
  }
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I must be misunderstanding, I placed your code in blah.awk, called it like: nawk -f blah.awk *.out in the correct dir where the *.out files are, and the output was quite unexpected..

$ nawk -f blah.awk *.out
Code 59
23
4
24
4
25
71
28
196
41
198
11
12
49
4
96
4
4
98
4
4
63
219
4
4
4
4
4
150
80
37
82
1389118
83
84
239
86
50
52
54
4
4
57
4
0
4
58
1
 
the code assumes the content of the files i slike that with the FIRST line having the DATE specification

Are ALL of your files look like this?
Code:
04/07/2004
0           5389
1              6
150          372
196           32
239           56
28             2
49             1
52            12
54             6
58            21
59             4
80             1
84            28
96           126
98             1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Ahhh, my problem was not the code, it was the blank line above the date in each .out file!

Thank you very much, your code worked just fine with 0 changes.. =)
 
functions as designed" [(c) development]

you're welcome.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top