![如何使用sed命令编辑nano的rcfile(界面元素颜色)](https://linux22.com/image/131474/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8sed%E5%91%BD%E4%BB%A4%E7%BC%96%E8%BE%91nano%E7%9A%84rcfile%EF%BC%88%E7%95%8C%E9%9D%A2%E5%85%83%E7%B4%A0%E9%A2%9C%E8%89%B2%EF%BC%89.png)
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