Arthmetic & Operations Using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arthmetic & Operations Using AWK
# 1  
Old 03-25-2012
Arthmetic & Operations Using AWK

Trying to do math while using awk and if statements.

Code:
awk 'BEGIN {FS = ":"} ; 
{if($2=1) {$3 +=};
print "Total employee work hours: " $3}' works_on.txt

The file format is
Code:
123456789:1:32.5 
123456789:2:7.5 
666884444:3:40.0 
453453453:1:20.0 
453453453:2:20.0 
333445555:2:10.0 
333445555:3:10.0 
333445555:10:10.0 
333445555:20:10.0 
999887777:30:30.0 
999887777:10:10.0 
987987987:10:35.0 
987987987:30:5.0 
987654321:30:20.0 
987654321:20:15.0 
888665555:20:0.0

Basically, I am trying to add the number in the 3rd field if the numbers in the second field are matching
Add the hours of 1. Which is 32.2 and 20.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-25-2012 at 04:58 PM.. Reason: code tags, please!
# 2  
Old 03-25-2012
Not sure what you want to add, but maybe like this:

Code:
awk -F ":" ' $2 == 1 { printf( "Total employee work hours: %.2f\n", $3 + 1 ); } ' input-file >output-file

# 3  
Old 03-25-2012
Each field in the file is specific. The first field is works SSN, the second field is the project number, and the third field is hours worked.
So, if someone ask for all the people working on project 1, I get all the hours then display the hours only.
SSN : P# : Hours

---------- Post updated at 02:45 PM ---------- Previous update was at 02:35 PM ----------

Quote:
Originally Posted by agama
Not sure what you want to add, but maybe like this:

Code:
awk -F ":" ' $2 == 1 { printf( "Total employee work hours: %.2f\n", $3 + 1 ); } ' input-file >output-file

I'm getting an error with this. Unterminated quoted string.
# 4  
Old 03-25-2012
Code:
awk -F: '{a[$2]+=$3}END {for(i in a) print "Total hours for project " i ": " a[i] }' myInputFile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 03-25-2012
Quote:
Originally Posted by vgersh99
Code:
awk -F: '{a[$2]+=$3}END {for(i in a) print "Total hours for project " i ": " a[i] }' myInputFile

How would I isolate for just say one project?
# 6  
Old 03-25-2012
Quote:
Originally Posted by Rapcher
How would I isolate for just say one project?
Code:
awk -F ":" -v project="${1:-1}" ' $2 == project { hours += $3; } END { printf( "total hours for project %d: %.2f\n", project, hours ); }' input-file



Assumes this is running as in a script where the first parameter is the project number. Sums the hours (column 3) for the indicated project and prints the total at the end.
This User Gave Thanks to agama For This Post:
# 7  
Old 03-26-2012
I was able to manipulate your code into what I needed. Thanks!

---------- Post updated at 07:53 AM ---------- Previous update was at 07:34 AM ----------

I got it working somewhat but its acting weird.

Code:
awk -F ":" ' $2==1 { hours += $3; } END { printf "Total Employee Hours: %.2f\n", hours }' works_on.txt

awk -F ":" ' $1==333445555 { count ++ } END { printf "Total number of dependents: " count }' dependent.txt

" "
exit

The input display comes up right next to the number for the second awk.

So it looks like this in terminal.
Code:
rapcher@rapcher-K53E:~$ ./extract
Total Employee Hours: 52.50
Total number of dependents: 3rapcher@rapcher-K53E:~$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

awk and &&

Hello Everybody. I am having trouble matching multiple strings within an instanza. I ONLY want it to print if it finds both strings within the range. Example input (newlines between groups inserted only for readibility): Keywords to match are "artist" and "play count" startgroup1 artist:... (5 Replies)
Discussion started by: sudo
5 Replies

3. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

4. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

5. Shell Programming and Scripting

Can BASH handle mathematical operations and do a Search & Replace?

Hello, I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it. I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers... (12 Replies)
Discussion started by: jl487
12 Replies

6. Shell Programming and Scripting

awk command: column operations

I have a txt file with two columns Freq Loss 10 30 20 40 30 10 40 50 50 60i have used the below code to get the minimum value out of this array in second cloumn. awk 'NR==N{min=$N;max=$N}NR>N{if ($N>max){max=$N};if ($N<min){min=$N}}END {print... (3 Replies)
Discussion started by: shashi792
3 Replies

7. Shell Programming and Scripting

Help With Array Operations in AWK

I have two files file1 A 2 4 6 8 B 1 3 5 7 C 1 3 5 7 D 1 3 5 7 E 1 3 5 7 file2 C D E F G H I J K L I need to add field 1 of file1 to both field 2s of file2 and repeat the same on all the rows in... (2 Replies)
Discussion started by: cold_Que
2 Replies

8. Shell Programming and Scripting

awk and &&

Hi I need to make some checks inside an awk statement, but cannot come up with the the syntax for several checks under one awk statement, for example: grep -w ${bits} mydata | awk '{if ($3==1)&&($4==3)&&($5==1) print }' awk: syntax error near line 1 awk: illegal statement near line 1 In... (3 Replies)
Discussion started by: aoussenko
3 Replies

9. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

10. Shell Programming and Scripting

Awk: Version && nextfile

How can I find which version of Awk is installed? OpSystem is HPUX 11.x I am getting an error when trying to use the keyword nextfile and I dont know why! (Well, I can only assume that I have am using a version of Awk that does not support nextfile. However, according to O'Reilly, nextfile is... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question