Description
- File1 contains exactly one section while File2
contains several sections.
- Each section begins with a line of the form
[section_name].
- Sections may be separated by none, one or more empty lines.
- We want to substitute the section specified in File1
for the same section in File2.
- File2 must start with a line declaring the beginning
of some section.
|
| Raw Input
| |
File1 contains the updated section.
| [section_2]
setting 2-1-NEW
setting 2-2-NEW
setting 2-3-NEW
|
|
| Raw Input
|
| Desired Output
| |
File2 is the original file.
| [section_1]
setting 1-1
setting 1-2
setting 1-3
[section_2]
setting 2-1-OLD
setting 2-2-OLD
setting 2-3-OLD
setting 2-4-OLD
[section_3]
setting 3-1
setting 3-2
[section_4]
setting 4-1
setting 4-2
setting 4-3
|
| [section_1]
setting 1-1
setting 1-2
setting 1-3
[section_2]
setting 2-1-NEW
setting 2-2-NEW
setting 2-3-NEW
[section_3]
setting 3-1
setting 3-2
[section_4]
setting 4-1
setting 4-2
setting 4-3
|
|
Script and Comments
Script1 [ 1] 1h
[ 2] 1d
[ 3] /^\[/!{
[ 4] H
[ 5] d
[ 6] }
[ 7] G
[ 8] s/^([^\n]*\n[^\n]*).*/\1/
[ 9] :0
[10] /^(\[[^]]*\])\n\1$/!{
[11] P
[12] $d
[13] N
[14] s/^.*\n(.*)\n(.*)/\2\n\1/
[15] b 0
[16] }
[17] s/\n.*//
[18] :1
[19] /\n(\[|$)/!{
[20] $!N
[21] $!b 1
[22] }
[23] /\n\[/!s/^.*//
[24] s/^.*\n//
[25] x
[26] G
[27] s/\n$//
[28] :2
[29] n
[30] b 2
| |