在多行匹配后追加文本行并在第一个多行匹配时停止
Append text line after multiline match and stop on first multiline match
我需要一个 sed 或 awk 命令,而不是脚本,即:
1) 按顺序匹配文件中的 2 个字符串:
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
这是必需的,因为任何单个字符串都可以在文件中出现多次。
但是其中两个这样的顺序字符串非常独特,可以匹配它们。
2) inserts/appends 在匹配的字符串后此文本行:
filter = ["a|sd.*|", "a|drbd.*|", "r|.*|"]
3) 在第一次匹配后停止处理并追加
因此,文本文件如下所示:
...
# filter = [ "a|^/dev/hda8$|", "r|.*/|" ]
#
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
# Configuration option devices/global_filter.
# Limit the block devices that are used by LVM system components.
# Because devices/filter may be overridden from the command line, it is
# not suitable for system-wide device filtering, e.g. udev and lvmetad.
# Use global_filter to hide devices from these LVM system components.
# The syntax is the same as devices/filter. Devices rejected by
# global_filter are not opened by LVM.
# This configuration option has an automatic default value.
# global_filter = [ "a|.*/|" ]
# Configuration option devices/types.
# List of additional acceptable block device types.
# These are of device type names from /proc/devices, followed by the
...
我需要这样的输出:
...
# filter = [ "a|^/dev/hda8$|", "r|.*/|" ]
#
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
filter = ["a|sd.*|", "a|drbd.*|", "r|.*|"]
# Configuration option devices/global_filter.
# Limit the block devices that are used by LVM system components.
# Because devices/filter may be overridden from the command line, it is
# not suitable for system-wide device filtering, e.g. udev and lvmetad.
# Use global_filter to hide devices from these LVM system components.
# The syntax is the same as devices/filter. Devices rejected by
# global_filter are not opened by LVM.
# This configuration option has an automatic default value.
# global_filter = [ "a|.*/|" ]
# Configuration option devices/types.
# List of additional acceptable block device types.
# These are of device type names from /proc/devices, followed by the
...
None 在 Whosebug 上找到的多行 sed 示例的示例对我有用。
我尝试了这个主题中的 F.Hauri 示例:
sed -e $'/^admin:/,/^$/{/users:/a\ NewUser\n}'
它在匹配独特的单词时工作正常,但不适用于像这样匹配连续的文本行:
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
并且在 sed 表达式中添加 '0 以在第一次匹配时停止在这种情况下不起作用。
更新说明以更好地描述目标。
awk '
/^\s+\# This configuration option has an automatic default value\./{
found=1
}
found && !flag && /\s+\# filter = \[ \"a\|\.\*\/\|\" \]/{
flag=1
[=10=]=[=10=] ORS ORS " filter = [\"a|sd.*|\", \"a|drbd.*|\", \"r|.*|\"]"
}
1
' test.conf > test.tmp && cp test.conf test.conf.bak && mv -f test.tmp test.conf
我需要一个 sed 或 awk 命令,而不是脚本,即:
1) 按顺序匹配文件中的 2 个字符串:
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
这是必需的,因为任何单个字符串都可以在文件中出现多次。 但是其中两个这样的顺序字符串非常独特,可以匹配它们。
2) inserts/appends 在匹配的字符串后此文本行:
filter = ["a|sd.*|", "a|drbd.*|", "r|.*|"]
3) 在第一次匹配后停止处理并追加
因此,文本文件如下所示:
...
# filter = [ "a|^/dev/hda8$|", "r|.*/|" ]
#
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
# Configuration option devices/global_filter.
# Limit the block devices that are used by LVM system components.
# Because devices/filter may be overridden from the command line, it is
# not suitable for system-wide device filtering, e.g. udev and lvmetad.
# Use global_filter to hide devices from these LVM system components.
# The syntax is the same as devices/filter. Devices rejected by
# global_filter are not opened by LVM.
# This configuration option has an automatic default value.
# global_filter = [ "a|.*/|" ]
# Configuration option devices/types.
# List of additional acceptable block device types.
# These are of device type names from /proc/devices, followed by the
...
我需要这样的输出:
...
# filter = [ "a|^/dev/hda8$|", "r|.*/|" ]
#
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
filter = ["a|sd.*|", "a|drbd.*|", "r|.*|"]
# Configuration option devices/global_filter.
# Limit the block devices that are used by LVM system components.
# Because devices/filter may be overridden from the command line, it is
# not suitable for system-wide device filtering, e.g. udev and lvmetad.
# Use global_filter to hide devices from these LVM system components.
# The syntax is the same as devices/filter. Devices rejected by
# global_filter are not opened by LVM.
# This configuration option has an automatic default value.
# global_filter = [ "a|.*/|" ]
# Configuration option devices/types.
# List of additional acceptable block device types.
# These are of device type names from /proc/devices, followed by the
...
None 在 Whosebug 上找到的多行 sed 示例的示例对我有用。
我尝试了这个主题中的 F.Hauri 示例:
sed -e $'/^admin:/,/^$/{/users:/a\ NewUser\n}'
它在匹配独特的单词时工作正常,但不适用于像这样匹配连续的文本行:
# This configuration option has an automatic default value.
# filter = [ "a|.*/|" ]
并且在 sed 表达式中添加 '0 以在第一次匹配时停止在这种情况下不起作用。
更新说明以更好地描述目标。
awk '
/^\s+\# This configuration option has an automatic default value\./{
found=1
}
found && !flag && /\s+\# filter = \[ \"a\|\.\*\/\|\" \]/{
flag=1
[=10=]=[=10=] ORS ORS " filter = [\"a|sd.*|\", \"a|drbd.*|\", \"r|.*|\"]"
}
1
' test.conf > test.tmp && cp test.conf test.conf.bak && mv -f test.tmp test.conf