Wie verwende ich sed zum Entfernen von (doppelten Anführungszeichen) in Codezeilen, während sie in echten Kommentarzeilen in einer vimrc-Datei verbleiben?

492
Chris Rainey

Auf Ubuntu (Server / Desktop) möchte ich ein einzelnes "Zeichen (doppelte Anführungszeichen) von den Zeilenanfang in meinem entfernen /etc/vim/vimrc.

Dies sollte für alle Zeilen erfolgen, die mit einem beginnen ", aber nicht, wenn "auf ein einzelnes Leerzeichen folgt, da letztere echte Kommentare angeben (im Gegensatz zu kommentiertem Code). Mein Ziel ist es, den kommentierten Code durch Entfernen des Starts "umzuschalten, während der Rest der Datei unverändert bleibt.

VOR:

" 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 

NACH DEM:

" 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:

~$ 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 

Bitte beachten Sie, dass bei einem eingerückten Code nach dem Start "eine Anzahl von Leerzeichen größer als eins folgt: Ich möchte auch diese Zeilen auskommentieren, wobei der Einzug erhalten bleibt.

Ich habe herausgefunden, wie man mit dem folgenden Befehl Dinge zum Laufen bringt:

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

Kann dies besser gemacht werden (sauberer / enger / etc.)?

Kann dies auch awkfür dasselbe Ergebnis gemacht werden?

0
Möglicherweise können Sie diese Frage verbessern. Es gibt eine Menge Probleme, aber wir erhalten die Meldung "Chris Rainey ist ein neuer Beitrag. Seien Sie nett und lesen Sie unseren Verhaltenskodex. Https://superuser.com/conduct" Hoffentlich kann Ihnen jemand antworten, dass es zu lange dauert. barlop vor 5 Jahren 0

1 Antwort auf die Frage

0
simlev

Der Ersetzungsbefehl ist sehr einfach:

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

Oder ähnlich in Perl:

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

Und AWK:

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

In Ihrem Beispiel scheinen Sie Ausnahmen für Zeilen anzugeben, die bestimmte Zeichenfolgen enthalten.
Dies wird nicht in Ihrem Text erklärt, könnte aber als Bedingung hinzugefügt werden:

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