0

I have a set of data looking like this:

1   0.1
2   0.23
3   0.05
5   0.73
6   0.40
7   0.34
10  0.11
11  0.45
12  0.68

I need to split the set of data to massives using the rule that the next value in the first column is bigger than previous one by more than 1, and find the maximum values in the second column for every of the massives. So the outcome in this case would be:

1   0.1
5   0.73
12  0.68

I tried couple things with NR parameter but apparently didn't succeed. So I will appreciate any help or advice you can give.

  • Surely the first output line should be: 2 0.23? – Thor Sep 16 '15 at 7:50
1
awk '
     $1 > count + 1{
         print max_n, max
         max = $2 - 1
     }
     max < $2{
         max = $2
         max_n = $1
     }
     {
        count = $1
     }
     END{
         print max_n, max
     }' file
  • I might be doing something wrong but it doesn't really work in my hands. Always giving error: awk: ./my.awk:2: awk ' awk: ./my.awk:2: ^ invalid char ''' in expression awk: ./my.awk:2: awk ' awk: ./my.awk:2: ^ syntax error – Kredo Sep 16 '15 at 10:16
  • @Kredo Reread script carefully: there is no "" char in it – Costas Sep 16 '15 at 11:59
  • it was ' ' ' not " " meaning that ' - is considered as invalid char. Have you tried running it? Did you save it as a separate awk script and then ran in a bush as following: ScriptName.awk file1 > file2 ? or you have done it in a different way? Could you provide a bit more details please? I desperately need it working :( – Kredo Sep 16 '15 at 12:36
  • ok looks like everything is working just fine after I removed ' chars from the beginning and end. Thanks a lot!!!! – Kredo Sep 16 '15 at 14:52
  • @Kredo Did you use right sha-bang #!//usr/bin/awk -f? Sure in the case you should store just all between ' excluded. – Costas Sep 16 '15 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.