Home » Infrastructure » Unix » Finding the Sum of a field from the flat file
Finding the Sum of a field from the flat file [message #139647] Thu, 29 September 2005 01:27 Go to next message
Naveen Verma
Messages: 60
Registered: August 2004
Member
Hi All
I am writing a shell script in which I have to find the sum of the salaries paid to employees in the company. The data available in a flat file.

let us assume the salary starts from the char 36 to 50.

Can anybody tell me a unix command to do this work

Your help will be greatly apprecialted.

Thanks in Advance
Naveen
Re: Finding the Sum of a field from the flat file [message #139693 is a reply to message #139647] Thu, 29 September 2005 03:52 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
oracle@mutation#awk -F"," '{ (total_sal=total_sal+$6)} {print $0 } END {print "Total sal " total_sal} ' emp.data
      7369,SMITH     ,CLERK    ,      7902,17-DEC-80,       800,          ,        20
      7499,ALLEN     ,SALESMAN ,      7698,20-FEB-81,      1600,       300,        30
      7521,WARD      ,SALESMAN ,      7698,22-FEB-81,      1250,       500,        30
      7566,JONES     ,MANAGER  ,      7839,02-APR-81,      2975,          ,        20
      7654,MARTIN    ,SALESMAN ,      7698,28-SEP-81,      1250,      1400,        30
      7698,BLAKE     ,MANAGER  ,      7839,01-MAY-81,      2850,          ,        30
      7782,CLARK     ,MANAGER  ,      7839,09-JUN-81,      2450,          ,        10
      7788,SCOTT     ,ANALYST  ,      7566,09-DEC-82,      3000,          ,        20
      7839,KING      ,PRESIDENT,          ,17-NOV-81,      5000,          ,        10
      7844,TURNER    ,SALESMAN ,      7698,08-SEP-81,      1500,         0,        30
      7876,ADAMS     ,CLERK    ,      7788,12-JAN-83,      1100,          ,        20
      7900,JAMES     ,CLERK    ,      7698,03-DEC-81,       950,          ,        30
      7902,FORD      ,ANALYST  ,      7566,03-DEC-81,      3000,          ,        20
      7934,MILLER    ,CLERK    ,      7782,23-JAN-82,      1300,          ,        10
Total sal 29025
Re: Finding the Sum of a field from the flat file [message #139716 is a reply to message #139693] Thu, 29 September 2005 05:13 Go to previous messageGo to next message
Naveen Verma
Messages: 60
Registered: August 2004
Member
Hi
Thanks, but a small issue.

The source file is not a csv file. The fields are of fixed length char strings, there is no commas to separate fields.

Can you gimme an example if the amount field starts at position 36 and ends at position 50.

Thanks,
Naveen
Re: Finding the Sum of a field from the flat file [message #139724 is a reply to message #139716] Thu, 29 September 2005 05:56 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
oracle@mutation#cat emp.data
7369SMITH CLERK    790217-DEC-80800
7499ALLEN SALESMAN 769820-FEB-811600
7521WARD  SALESMAN 769822-FEB-811250
7566JONES MANAGER  783902-APR-812975
7654MARTINSALESMAN 769828-SEP-811250
7698BLAKE MANAGER  783901-MAY-812850
7782CLARK MANAGER  783909-JUN-812450
7788SCOTT ANALYST  756609-DEC-823000
7839KING  PRESIDENT    17-NOV-815000
7844TURNERSALESMAN 769808-SEP-811500
7876ADAMS CLERK    778812-JAN-831100
7900JAMES CLERK    769803-DEC-81950
7902FORD  ANALYST  756603-DEC-813000
7934MILLERCLERK    778223-JAN-821300
oracle@mutation#awk  '{ (total_sal=total_sal+ substr($0,33,4))} {print $0 } END {print "Total sal " total_sal} ' emp.data
7369SMITH CLERK    790217-DEC-80800
7499ALLEN SALESMAN 769820-FEB-811600
7521WARD  SALESMAN 769822-FEB-811250
7566JONES MANAGER  783902-APR-812975
7654MARTINSALESMAN 769828-SEP-811250
7698BLAKE MANAGER  783901-MAY-812850
7782CLARK MANAGER  783909-JUN-812450
7788SCOTT ANALYST  756609-DEC-823000
7839KING  PRESIDENT    17-NOV-815000
7844TURNERSALESMAN 769808-SEP-811500
7876ADAMS CLERK    778812-JAN-831100
7900JAMES CLERK    769803-DEC-81950
7902FORD  ANALYST  756603-DEC-813000
7934MILLERCLERK    778223-JAN-821300
Total sal 29025
Re: Finding the Sum of a field from the flat file [message #139896 is a reply to message #139724] Fri, 30 September 2005 00:53 Go to previous message
Naveen Verma
Messages: 60
Registered: August 2004
Member
Thank You
Previous Topic: server alerts
Next Topic: Installing 10g onAIX 5.3 1st Prob
Goto Forum:
  


Current Time: Tue Apr 23 07:05:04 CDT 2024