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