Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bash to transform a numeric sequence

Tags:

bash

shell

r

Here is a vector of numbers I have which ascend (it is called t8_)

2 7 8 9 11 15 34 91 91 92 94

the problem is that some number happen two times - I don't want to remove them but they have to become two numbers which are unique and not floating point. So I thought about at first iterating through the vector

the next vector would be

2 7 8 9 11 15 34 91 92 92 94

then the same process again

when the vector would be

2 7 8 9 11 15 34 91 92 93 94

For the example this works

#!/bin/bash

while read -a vec; do
        # INT_MIN for bash: 32-bit bash also supports 64-bit integers.
        min=$((-1<<63)) 
        for ((i = 0; i < ${#vec[@]}; i++)); do
                (( min = vec[i] = vec[i] > min ? vec[i] : min + 1 ))
        done
        echo "${vec[@]}"
done
###

The problem is that it does not for my real vector which looks like that https://www.dropbox.com/s/vp67eiw4ns9rr07/num?dl=0.

When I do un.sh < vec I just don't get any output in this specific instance. Could somebody tell me why?

like image 964
ktoui Avatar asked Jul 14 '26 21:07

ktoui


1 Answers

I can't say why your shell code isn't working for you, but I can offer you a Perl solution that works fine with your data

use strict;
use warnings;
use 5.010;
use autodie;

my @numbers = split ' ', do {
    open my $fh, '<', 'num';
    local $/;
    <$fh>;
};

for my $i ( 1 .. $#numbers ) {
  ++$numbers[$i] if $numbers[$i] == $numbers[$i-1];
}

say "@numbers";
like image 123
Borodin Avatar answered Jul 18 '26 06:07

Borodin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!