ksh field math


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh field math
# 1  
Old 07-18-2014
Debian ksh field math

Hi,
I have a text like with many rows of data like:
Code:
7a,0,1182
7a,1,1040
7b,0,483
7b,1,242
7c,0,224
7c,1,877

I need to be able to take the value of the first field (i.e. 7a) and if there are multiples, add the value of the third field, (1182 + 1040) and insert the output of all of this in a new text file. So the final output would be:
Code:
7a,2222
7b,725
7c,1101

This needs to use ksh.
Thanks!!

Last edited by Scrutinizer; 07-18-2014 at 10:12 PM.. Reason: code tags
# 2  
Old 07-18-2014
With awk this is straightforward:
Code:
awk '{A[$1]+=$3} END{for(i in A) print i,A[i]}' FS=, OFS=, file

You can call this from ksh.

If you want to do this in ksh only, can you post what you have tried and where you are stuck?
Also what is your OS and version and is this ksh88 or ksh93 ?
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-18-2014
Also let us know if (as in your example) all occurrences of a given value in field 1 are on contiguous lines in your input text file.

And, please use CODE tags when posting code segments, sample input data, and sample output data instead of depending on moderators to clean up your messages for you.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look... (11 Replies)
Discussion started by: lawsongeek
11 Replies

2. Shell Programming and Scripting

Manipulating field differences using ksh

I have a list of files that should contain the following Im trying to find the items of interest that are missing from each file and create a csv. cat *.txt | while read file do grep 3500 file | tr '\012' ',' done My problem is this possible output one.txt ... (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Math action in ksh

Hello, I want to do div action in ksh and get the full result(with leftover). i.e. 4/3=1.33 and not 4/3=1 (4 Replies)
Discussion started by: LiorAmitai
4 Replies

4. Shell Programming and Scripting

ksh date math

I have a requirement to remove all files from a directory execpt those > (now -N hrs). Once I can figure out how to calculate (now -N hrs) I was thinking of creating a temporary file with that time (now-N hrs) than using the find and -newer option. Does anybody have any KSH code (needs to... (3 Replies)
Discussion started by: BeefStu
3 Replies

5. Shell Programming and Scripting

Assign field value in a .csv file to a variable using ksh

Hi, I'm new to the scripting world... I want to know that how can I assign the the field value(that has multiple lines) of a .csv file to a variable??? Model of my .csv file : 1 Poppy 5 2 red 6 3 black 5 4 white 8 and so on,the list... (4 Replies)
Discussion started by: srim
4 Replies

6. Shell Programming and Scripting

Why won't my Ksh do math with (( ))?

Hello, I'm usring Ksh on AIX 5.3. For some reason my K-Shell gives me an error when I try to use the math operators (( )). Can anyone tell me what's going on and how to fix it? Thanks so much! My K-Shell: />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10... (2 Replies)
Discussion started by: troym72
2 Replies

7. Shell Programming and Scripting

math help

$ x=1 $ y=1.5 $ z=$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong? (2 Replies)
Discussion started by: rockbike
2 Replies

8. Programming

something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field I got header file <math.h> included in my .c file , then I write the code as below: k = sqrt(i); /* both variables k and i are int */ then I cc temp.c it says like this undefined... (4 Replies)
Discussion started by: blf0
4 Replies
Login or Register to Ask a Question