help with doing calculations on data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help with doing calculations on data
# 1  
Old 03-21-2011
Lightbulb help with doing calculations on data

Dear All,
I have a long list like this:

Code:
337
375
364
389
443
578
1001
20100
.
.
.
.

etc

I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like

Code:
337       0
375      -38
364      -27
389      -52
443      -106
578      -241     
1001    -664
20100  -19763
.
.
.
.

etc

Can someone shed some light on how to accomplish this ... to be more precise the first column is actually a feild entry in my file ...... how do I perform calculations on this particular feild and report it as a new column ?

Thanks for your input in advance Smilie


Thanx
# 2  
Old 03-21-2011
I think it would be more useful if you provided sample of the real file's data. For this data:
Code:
awk 'NR==1{x=$1}{$2=x-$1}1' file


Last edited by bartus11; 03-21-2011 at 11:48 AM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-21-2011
OK Bartus11,
Here is an part of the real file ....

Code:
common.levure5-6-7-8_SNP_SNP_annotated.gff      file_1  1348
common.levure5-6-8_SNP_SNP_annotated.gff        file_2  1167
common.levure5-6-7_SNP_SNP_annotated.gff        file_3  1125
common.levure5-7-8_SNP_SNP_annotated.gff        file_4  1104
common.levure6-7-8_SNP_SNP_annotated.gff        file_5  1083
common.levure5-6_SNP_SNP_annotated.gff          file_6  952
common.levure5-8_SNP_SNP_annotated.gff          file_7  896
common.levure5-7_SNP_SNP_annotated.gff          file_8  886
common.levure6-8_SNP_SNP_annotated.gff          file_9  872
common.levure7-8_SNP_SNP_annotated.gff          file_10 862
common.levure6-7_SNP_SNP_annotated.gff          file_11 846


So basically what I wanted to do was more complicated ... I give you example output of first 2 lines .... First I want to calculate the number of instances a DIGIT is present in column 1 (which I think is $1) ... so in the first line there are 4 digits 5-6-7-8 and in the lines 2 to 5 there are 3 digits and in the other lines 2 digits (by digits I mean numeric characters) then these values for each line need to be used to divide the last field of the file .... so in line one 1348 should divide by 4, in line two 1167 should divide by 3 and so on. The the results of the division should be displayed in the next column and its not over .... then the first entry of the division which is 337 (obtained by 1348/4) has to be used as a constant to subtract all other entries in the same column below it and reported in yet another column...

my expected output for the first 2 lines as an example is ....

Code:
common.levure5-6-7-8_SNP_SNP_annotated.gff      file_1  1348    337    0
common.levure5-6-8_SNP_SNP_annotated.gff        file_2  1167     389    -52

etc

I just didnt want to ask so many questions so I thought just ask you the last step .... but if you can explain how to do the above especially how to scan for elements of choice in a single field as counting numeric digits in the first feild in this example, that will be a great thing to learn.

Thank you and please make comments on the code if you can kindly provide one

Cheers Smilie
# 4  
Old 03-21-2011
using shell script:

make a shell script (shell.sh) which takes 1 argument (data filename)
Code:
count=0
for i in `cat $1`
do
count=`expr $count + 1`
if [ $count -eq 1 ]
then
top=$i
fi
echo $i `expr  $top - $i`

done

then run the script,
Code:
./shell.sh filename

This User Gave Thanks to gaurab For This Post:
# 5  
Old 03-21-2011
Code:
perl -lnae '$n=$F[0]=~s/\d/$&/g;$div=$F[2]/$n;$first=$div if $.==1;print "$_\t$div\t" . ($first-$div)' file

You can find description of most of the code used here in https://www.unix.com/302503405-post6.html
New things:
$n=$F[0]=~s/\d/$&/gcount number of numeric characters contained in first field and store this count in $n
$first=$div if $.==1assign $div value to $first when first line is being processed ($. contains number of currently processing line)
Rest of the code is just arithmetic operations and output formatting.
This User Gave Thanks to bartus11 For This Post:
# 6  
Old 03-21-2011
@gaurab: Thanks a lot

@Bartus11: You always amaze me with your perl one-liners .... simply awesome and thanks very much for the comments too ..... Honestly u need to tell me / give directions to resources and of how and where I can learn some of this .. Have a great day Smilie
# 7  
Old 03-21-2011
"Learning Perl" by Tom Phoenix, Randal L. Schwartz is a good start Smilie BTW here is the same logic in AWK:
Code:
awk '{x=$1;n=gsub("[0-9]","",x);div=$3/n;if (NR==1) first=div; print $0"\t"div"\t"first-div}' file

This User Gave Thanks to bartus11 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop doing calculations

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade for each student based on the scores and categories in the file. I am able to get the script to run the only issue however is that the same score for each student is the same. I'm self-teaching myself the... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Linux - Calculations between multiple rows of data

Morning All, I am needing assistance with a calculation process, which performs calculations on a group of records. Here is a breakdown of my requirement: Col1 = Always same value. Col2 = Grouping Column, and will have the same value for 5/6/7 records for example. Col3 = Date Col4 =... (3 Replies)
Discussion started by: RichZR
3 Replies

3. Shell Programming and Scripting

Output calculations

Attached are the is original output (zipped file) and a custom file using the awk code below in which the average reads per bait are calculated (average.txt) awk '{if(len==0){last=$4;total=$6;len=1;getline}if($4!=last){printf("%s\t%f\n", last,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Number calculations

I'm writing a script that will read all the fields of a text file into an array(if they are numeric), while at the same time computing the minimum and maximum values from the file. After that I want to output the average of all the numbers in the array. The first problem I'm having is that many... (10 Replies)
Discussion started by: ksmarine1980
10 Replies

5. Shell Programming and Scripting

Problem with calculations

grep Quality abc.txt | awk -F"=" '{print $2}' o/p is given as 70/70 49/70 I want in the below format (percentage format) 100% 70% help me!!!!:confused::confused::confused: ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Cell 01 -... (3 Replies)
Discussion started by: nikhil jain
3 Replies

6. UNIX for Dummies Questions & Answers

Doing calculations with bc on one field

Hello, I have to turn: Apple Inc.:325,64:329,57 into Apple Inc.:325,64:329,57:3,93 3,93=329,57-325,64. My code: cat beurs.txt | sed 's/\(*\):\(*\),*\(*\):\(*\),\(*\)/\4\.\5-\2\.\3/' beurs.txt | bc| tr '.' ',' | sed 's/^-*,/0,/' > winstmarges.txt; paste -d: beurs.txt winstmarges.txt; rm... (5 Replies)
Discussion started by: ikke008
5 Replies

7. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

8. UNIX for Dummies Questions & Answers

Date Calculations

I need to be able to use the current date and calculate 7 days ago to be stored in another variable to be passed to a file in my Unix shell script. I need the date in the following format: date '+%m/%d/%Y' or 05/16/2006 How do I calculate date minus 7 days or 1 week ago? (8 Replies)
Discussion started by: mitschcg
8 Replies

9. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

10. UNIX for Dummies Questions & Answers

Float calculations

As expr is used for integer calculations, which command is used for float calculations. (1 Reply)
Discussion started by: sharmavr
1 Replies
Login or Register to Ask a Question