如何使用 sed 删除代码行上的“(双引号)同时将它们保留在 vimrc 文件中的真实注释行上?

如何使用 sed 删除代码行上的“(双引号)同时将它们保留在 vimrc 文件中的真实注释行上?

在 Ubuntu(服务器/桌面)上,我希望从"./etc/vim/vimrc

应该对以 开头的所有行执行此操作",但如果"后面跟着 则不必执行此操作单身的空格字符,因为后者表示真实的注释(与注释代码相对)。我的目标是通过删除开头的 来切换注释代码",同时保持文件其余部分不变。

前:

" Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc.
" This happens after /etc/vim/vimrc(.local) are loaded, so it will override
" any settings in these files.
" If you don't want that to happen, uncomment the below line to prevent
" defaults.vim from being loaded.
" let g:skip_defaults_vim = 1

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
  syntax on
endif

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
"  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif

后:

" Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc.
" This happens after /etc/vim/vimrc(.local) are loaded, so it will override
" any settings in these files.
" If you don't want that to happen, uncomment the below line to prevent
" defaults.vim from being loaded.
" let g:skip_defaults_vim = 1

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
  syntax on
endif

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

差异:

~$ diff BEFORE.out AFTER.out 
21c21
< "set background=dark
---
> set background=dark
25,27c25,27
< "if has("autocmd")
< "  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
< "endif
---
> if has("autocmd")
>   au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
> endif

请注意,如果有缩进的代码,则开头后面"会跟着多个空格字符,个数大于一个:我想在保留缩进的同时取消注释这些行。

我弄清楚了如何使用以下命令来使事情正常工作:

$ sudo sed -i.orig '/^\" [a-zA-Z]\|^"set compatible\|^\" let g:skip_defaults_vim = 1b/! s/^\"//' /etc/vim/vimrc

可以做得更好吗(更干净/更紧密/等等)?

也可以用这种方法得到awk相同的结果吗?

答案1

替换命令非常简单:

sed -r 's/^"(\S|\s{2,})/\1/' /etc/vim/vimrc

或者在 Perl 中类似地:

perl -lape 's/^"(\S|\s{2,})/\1/' /etc/vim/vimrc

和 AWK:

awk '{$0=gensub(/^"(\S|\s{2,})/,"\\1",1)}1' /etc/vim/vimrc

在您的示例中,您似乎指定了包含某些字符串的行的例外情况。
您的文本中没有解释这一点,但可以将其添加为条件:

sed -r '/^"set compatible/! s/^"(\S|\s{2,})/\1/' /etc/vim/vimrc
perl -lape 's/^"(\S|\s{2,})/\1/ if!/^"set compatible/' /etc/vim/vimrc
awk '!/^"set compatible/ {$0=gensub(/^"(\S|\s{2,})/,"\\1",1)}1' /etc/vim/vimrc

相关内容