jarhead
jarhead

Reputation: 59

Inserting sequential numbers and line numbers for certain lines

I'm attempting to automate the following process:

  1. Insert a number on line 1 (0001)
  2. For the next 25 lines, insert sequential line numbers (1-25) along with two spaces
  3. After line number 25, continue the next sequential number from line 1 (0002)
  4. Do step 2 again
  5. After this line number 25, continue sequentially (0003)
  6. Do step 2 again
  7. Etc.

The lines containing sequential numbers (0001, 0002, etc.) would contain just the number so I'm assuming adding a carriage return would be needed.

Is it also possible when numbering lines to include a space before numbers 1-9 so they line up with 10-25?

So something like...

0001
 1  This is text
 2  Some more text
 ↓
 9  Text here
10  Text here
 ↓
24  Additional text
25  And some more text
0002
 1  Text again
 2  More text
 ↓
 9  Text here
10  Text here
 ↓
24  Additional text
25  And some more text
0003

Upvotes: 0

Views: 491

Answers (2)

Kent
Kent

Reputation: 195179

interesting question, I got this line, which gives required formatted output:

awk '{s=(NR-1)%25}!s{printf "%04d\n", ++k}{printf "%2d %s\n",s+1,$0}' file

Upvotes: 1

BMW
BMW

Reputation: 45293

using awk

awk 'FNR%25==1{printf "%04d\n",++i;s=1}{print s++,$0}' file

Upvotes: 2

Related Questions