Description
In the following example, every occurrence of [0-9]+ except the first one in a file will be enclosed with a pair of angle brackets. This script can be also used to:
  • replace all but the first occurrence with some string.
  • remove all but the first occurrence.
Note that it is possible that there are some other occurrences in the same line with the first one.
Raw Input Desired Output
First number 1111 Second 2222
Third number 333 number
Fourth
44444 number Fifth number 55555
Final number
777
First number 1111 Second <2222>
Third number <333> number
Fourth
<44444> number Fifth number <55555>
Final number
<777>
Script and Comments
Script1
[ 1] /[0-9]+/!b
[ 2] s/[0-9]+/&\n/
[ 3] h
[ 4] s/^[^\n]*\n//
[ 5] s/[0-9]+/<&>/g
[ 6] G
[ 7] s/^([^\n]*)\n([^\n]*)\n.*/\2\1/
[ 8] :loop
[ 9] n
[10] s/[0-9]+/<&>/g
[11] b loop
Comments -r
  1. Steps [2] thru [7] is used to process the line where the first occurrence resides:
    • In Step [2], a newline character is inserted after the first occurrence, which is used as a mark. That mark divides the line into two parts: the first one containing the first occurrence and the second one.
    • Step [3] saves the contents of PS to HS. After this, HS consists of the first part, a newline, and the second part.
    • Step [4] deletes the first part from PS.
    • Step [5] performs replacements on the second part.
    • Step [6] appends the data saved by Step [3] to HS. After this, PS contains
      • the modified second part,
      • a newline,
      • the first part,
      • a newline, and
      • the original second part.
    • Step [7] makes PS consisting of the first part followed by the modified second part.
    • Steps [8] thru [11] constitute a loop where each iteration will
      • prints the current line and reads the next one,
      • then perform replacements.
    • To replace occurrences other than the first one with some string, change the replacement part of `s' commands in Steps [5] and [10] to the desired one, or an empty string to remove them.