nanorc
这是我的文件的示例:
(...)
## Detect word boundaries differently by treating punctuation
## characters as parts of words.
# set wordbounds
## The characters (besides alphanumeric ones) that should be considered
## as parts of words. This option does not have a default value. When
## set, it overrides option 'set wordbounds'.
# set wordchars "<_>."
## Paint the interface elements of nano. These are examples;
## by default there are no colors, except for errorcolor.
# set titlecolor brightwhite,blue
# set statuscolor brightwhite,green
# set errorcolor brightwhite,red
# set selectedcolor brightwhite,magenta
# set numbercolor cyan
# set keycolor cyan
# set functioncolor green
## In root's .nanorc you might want to use:
# set titlecolor brightwhite,magenta
# set statuscolor brightwhite,magenta
# set errorcolor brightwhite,red
# set selectedcolor brightwhite,cyan
# set numbercolor magenta
# set keycolor brightmagenta
# set functioncolor magenta
## Setup of syntax coloring.
##
## Format:
##
## syntax "short description" ["filename regex" ...]
(...)
我需要将## Paint the interface elements of nano.
块更改为:
## Paint the interface elements of nano. These are examples;
## by default there are no colors, except for errorcolor.
set titlecolor brightwhite,blue
set statuscolor brightwhite,green
set errorcolor brightwhite,red
set selectedcolor brightwhite,magenta
set numbercolor cyan
set keycolor cyan
set functioncolor green
换句话说,取消注释第一个颜色块并删除第二个颜色块(.nanorc
仅建议用于根)。
如何使用 sed 命令实现此目的?如果多个 sed 命令(而不是一个大命令)使其变得简单且易于阅读,我更喜欢这样做。
答案1
尝试:
sed "/## Paint the interface elements of nano./,/^$/ \
{s/^# //; /## In root's .nanorc you might want to use:/,/^$/d}" infile
这将查找## Paint the interface elements of nano.
第一个空行和第一个空行之间的行,然后如果以 hash-space 开头,则取消注释这些行#
,并删除以 hash-space 开头的行之间的所有其他内容,## In root's .nanorc you might want to use:
直到首先在同一块中找到空行,我们曾经{...}
应用过该行sed 脚本只打印我们需要的块,如果与默认操作sed
的条件不匹配,将打印其他所有行。sed