Basic Bash algorithm with sum/subtraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Bash algorithm with sum/subtraction
# 1  
Old 03-16-2017
Basic Bash algorithm with sum/subtraction

Hi all,
i'm making some test on a data file. Imagine i have two columns inside it :

Code:
80377,20

80377,20

80379,19

80378,20

80380,20

80382,20

80381,21

Just to understand how can it works, imagine to subtract 100 to the number in the first column when the other one in the second column changes by one. For example >> if changes from 20 to 21 then add 100 to 80381, if it changes from 20 to 19 subtract 100 to 80381..that's it.

Any idea it can be done with a shell script while reading a whole file?

Thanks in advance .

Last edited by Board27; 03-16-2017 at 02:59 PM..
# 2  
Old 03-16-2017
A few things to note here:-
As you are using 'bash' your data shows us pseudo-floating point arithmetic.
Are we to assume that anything beyond the floating point can be ignored?
If it can't be ignored do we round up or down, or both depending on the floating point value?
Why are there commas, (field separators?), on the last two lines?
# 3  
Old 03-16-2017
Hi,
of course, i've corrected the output as it should be.
thank you
# 4  
Old 03-16-2017
So you have every other an empty line. What should your output look like? Does it have to be shell syntax, or would awk, sed, ..., do as well?

Last edited by RudiC; 03-16-2017 at 04:40 PM..
# 5  
Old 03-16-2017
Well, shooting in the dark, try
Code:
while IFS=, read NR SC; do [ "$NR" != "" ] && { ((NR+=100*(SC-${OLDSC:-$SC}))); OLDSC=$SC; echo $NR,$SC; } || echo; done < file

These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. UNIX for Beginners Questions & Answers

Sum talk time in bash

Hello, guys ! I need a little help :) Lets say i have log files with "n" calls like this one : """"" <+825080825462>","+825080825462","60197774588","from-internal", "SIP/518-00013e14","SIP/eu.test.com_outgoing-00013e15","Dial",... (4 Replies)
Discussion started by: dragonfly85
4 Replies

3. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

4. Shell Programming and Scripting

Bash Sum up for spezific Numbers

Hello everyone, I need your assistance with bash. I want to sum up some numbers, that's not the big problem i think, but the values i want to some depending on another number. For example, I have a file with some rows of content. Number 1, Number 2, other content. I want to sum up number... (4 Replies)
Discussion started by: Bruder_Bruno
4 Replies

5. Shell Programming and Scripting

Help in making a basic bash script

Hi All, I am trying to monitor CPU load of few processes, with the same name. The output that I get from top is the following 28171 root 20 0 1089m 21m 3608 S 103 0.3 15:16.89 /opt/ppp//h264rtptranscoder.bin --videoPort=14504 --audioPort=14505 27589 root 20 0 1060m 23m... (3 Replies)
Discussion started by: liviusbr
3 Replies

6. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

7. UNIX for Dummies Questions & Answers

Basic Unix bash script help

Hello there Been using Unix bash scripting for two days now so am very new to this. I am currently doing a project now and i'm basically making a noughts and crosses game (or tic tac toe). I have created the board using an array. When I try and check to see if the array is empty using an If... (3 Replies)
Discussion started by: ChrisHoogie
3 Replies

8. Shell Programming and Scripting

subtraction in bash arrays

hi i am using bash shell to perform some subraction. here is what i have: i have a while loop and am using i as a counter. diff= `expr ${ARRAY1} - ${ARRAY2}` for example array1 has -0.7145 and array2 has -0.7041. when i try the above command, i get expr: non-numeric argument. any... (6 Replies)
Discussion started by: npatwardhan
6 Replies

9. Shell Programming and Scripting

bash script, pattern matching + sum

Hi guys, i have been struggling to achieve the following and therefor looking for some help. I am trying to write something that will summerize the following output from my switches for daily totals. Basicly if $1 $2 $3 doesnt change, we can keep adding up $4. Probably would use a awk print end... (3 Replies)
Discussion started by: Wratholix
3 Replies

10. Shell Programming and Scripting

Basic bash 'for loop' usage

Hi! I have a simple question about using a for loop. I'm trying to open up all the zip files in the currect directory with ark, but I am getting the error "bash: syntax error near unexpected token `for $i ; do ark $i ; done ; I looked in the info pages for bash, but I can't seem to figure... (2 Replies)
Discussion started by: Orange Stripes
2 Replies
Login or Register to Ask a Question