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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

awk '{print $2"-"$3}' file.txt

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
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


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.