Skip to main content
added 8 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

The following awk program would produce two "rulers", each consisting of a line of digits. The second line will be numbered from 1 to 10 (with 0 taking the place of 10) with no intermediate spacing between the digits. The first line will be numbered in the same way,similarly but with some spacing between each digit.

This essentially adds the next digit to the line2 string (the second ruler) in each iteration of the while loop, while adding a right-adjusted field of width step with a digit to the line1 string (the first ruler) every step iteration.

There is no width restriction in this code, and no sanitation or validation of the command line arguments.

The following awk program would produce two "rulers", each consisting of a line of digits. The second line will be numbered from 1 to 10 (with 0 taking the place of 10) with no intermediate spacing between the digits. The first line will be numbered in the same way, but with some spacing between each digit.

This essentially adds the next digit to the line2 string (the second ruler) in each iteration of the while loop, while adding a right-adjusted field of width step with a digit to the line1 string (the first ruler) every step iteration.

The following awk program would produce two "rulers", each consisting of a line of digits. The second line will be numbered from 1 to 10 (with 0 taking the place of 10) with no intermediate spacing between the digits. The first line will be numbered similarly but with some spacing between each digit.

This essentially adds the next digit to the line2 string (the second ruler) in each iteration of the while loop, while adding a right-adjusted field of width step with a digit to the line1 string (the first ruler) every step iteration.

There is no width restriction in this code, and no sanitation or validation of the command line arguments.

added 8 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
$ ./scriptruler 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890 $ ./scriptruler 72 1 2 3 4 5 6 7 8 9 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./scriptruler 72 10 1 2 3 4 5 6 7 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./scriptruler 122 6 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 

ScriptThe ruler script:

This essentially adds the next digit to the line2 string (the second ruler) in each iteration of the while loop, while adding a right-adjusted field of width step with a digit to the line1 string (the first ruler) every step iteration.

$ ./script 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890 $ ./script 72 1 2 3 4 5 6 7 8 9 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./script 72 10 1 2 3 4 5 6 7 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./script 122 6 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 

Script:

$ ./ruler 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890 $ ./ruler 72 1 2 3 4 5 6 7 8 9 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./ruler 72 10 1 2 3 4 5 6 7 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./ruler 122 6 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 

The ruler script:

This essentially adds the next digit to the line2 string (the second ruler) in each iteration of the while loop, while adding a right-adjusted field of width step with a digit to the line1 string (the first ruler) every step iteration.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

The following awk program would produce two "rulers", each consisting of a line of digits. The second line will be numbered from 1 to 10 (with 0 taking the place of 10) with no intermediate spacing between the digits. The first line will be numbered in the same way, but with some spacing between each digit.

The program takes two optional command-line arguments. The first argument is the total width of the second ruler (80 by default), while the second argument is the step size, or tab width, of the numbering of the first ruler (8 by default, as this is the common tab width on Unix terminals).

Example runs:

$ ./script 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890 $ ./script 72 1 2 3 4 5 6 7 8 9 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./script 72 10 1 2 3 4 5 6 7 123456789012345678901234567890123456789012345678901234567890123456789012 $ ./script 122 6 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 

Script:

#!/usr/bin/awk -f BEGIN { width = ARGC >= 2 ? ARGV[1] : 80 step = ARGC >= 3 ? ARGV[2] : 8 while (++i <= width) { if (i%step == 0) line1 = line1 sprintf("%*s", step, (i/step)%10) line2 = line2 (i%10) } print line1 print line2 }