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!

Unix Scripts for Flat file

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
CA
Hello,

I have a sample flat file as below. Note that it is delimited by |.

-the object is write a little script read the file.
-select the distinct fileds.
-group by 4th abd 5th fields and do the sum on 10th field.

N|3|10|2461|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-.70|0|0| | |N
N|3|10|2461|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |+.70|0|0| | |N
N|3|10|2461|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-.70|0|0| | |N
N|3|10|2462|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-37.80|0|0| | |N
N|3|10|2462|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |+37.50|0|0| | |N
N|3|10|2462|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-37.50|0|0| | |N
N|3|10|2462|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |+37.50|0|0| | |N


Hence, I want the result to look like the following:
N|3|10|2461|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-00.70|0|0| | |N
N|3|10|2462|0003|25092004|P2K PAYROLL SUMMARY|PAYROLL 24092004| |-00.30|0|0| | |N

Has anyone being able to the this before.

Please help.
 
nawk -f jay.awk sampleFile

here's jay.awk:

Code:
BEGIN {
  FS=OFS="|"

  FLDkey1="4"
  FLDkey2="5"

  FLDsum="10"
}

{
   idx = $FLDkey1 SUBSEP $FLDkey2
   if ( idx in arrSum ) {
      split(arrSum[idx], a, FS);
      $FLDsum += a[FLDsum];
   }
   arrSum[idx] = $0;
}

END {
  for (i in arrSum)
      print arrSum[i];
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanx for your help. I created a little shell script like the folowing mainly to accomplish the following:
1)Go to my local windows directory.
2)Ftp the file and rename it to payroll.txt
3)run the jay.awk which I have renamed to payroll.awk
4)copy the result.txt to interface directory.
5)put a timestamp on the copy of current result.txt in our current directory
6)move currrent results.txt to old directory.

Here is the copy of my shell scripts


#!/bin/sh
`ftp c:\\payroll\\files`
`get payroll.txt`
awk -f payroll.awk payroll.txt > results.txt
`cp results.txt interface\`
`results%D %n%t.txt`
`mv results* old`

However, when I tried above there is all kind of errors. Please help if you could.

Assuming this shell scritps is working. How would i put its executable, as icon, on a user desktop.
That is an icon on the user's windows desktop calling/executing a unix shell script.

Again, thanx for all your help.
 
I'd suggest starting here: faq80-772
and maybe reading other FAQs in this group.

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

Part and Inventory Search

Sponsor

Back
Top