Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've got a file with two columns corresponding to start-end ranges like this:

awk '{print $2"-"$3}' file.txt
91082802-91082990
94018177-94018321
146945732-146945949
88701397-88701594
80922510-80922643
28800551-28800680
23612818-23613016
69178478-69178630
107524531-107524669
73153734-73154149
[...]

I would like to expand the start-end ranges to up to 500%, except if the range goes beyond a certain value, N=1000. If the start-end range is already above N=1000, I want to rewrite the start-end to a n=1000 window around the centre.

How can I do that with an awk/bash one-liner?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I came upon this one liner:

awk '{len=$3-$2+1; mid=($2+$3)/2;
      if (len>1000) {printf "%i-%i\n", mid-500, mid+499}
      else {newlen=len*5>1000?1000:len*5;
            printf "%i-%i\n", mid-(newlen/2), mid+newlen/2-1}}'
share|improve this answer
    
Hardly a one-liner. An obvious simplification is to remove the if part: len>1000 is a subcase of len*5>1000. I'm not sure I understand the requirement though. –  Gilles Jul 15 '11 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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