1

I would like to use awk to work on some files. I have a file like this:

input:

a 2 16 b 17 25 c 26 32 d 33 51 

and what I want is to split all rows to enumerate the range between column 2 and column 3, incrementing the numbers by 2. For example:

a 2 4 6 8 10 12 14 16 b 17 19 21 23 25 c 26 28 30 32 d 33 35 37 39 41 43 45 47 49 51 
3
  • 3
    It's just a simple for loop that increments by 2. Commented Jan 27, 2015 at 17:37
  • 1
    Please show your attempt, we're not here to do your work for you. If you can't get it working, we'll help you fix it. Commented Jan 27, 2015 at 17:38
  • 1
    what should happen if $2 is odd and $3 is even, for example, so the range isn't a multiple of 2? Commented Jan 27, 2015 at 20:13

1 Answer 1

4
$ awk ' { printf("%-3s", $1) > for (i=$2; i<=$3; i+=2) > printf ("%-3d", i) > printf("\n") } ' <<EOF > a 2 16 > b 17 25 > c 26 32 > d 33 51 > EOF a 2 4 6 8 10 12 14 16 b 17 19 21 23 25 c 26 28 30 32 d 33 35 37 39 41 43 45 47 49 51 
Sign up to request clarification or add additional context in comments.

1 Comment

You can use print "" in place of printf("\n") -- a little tidier.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.