Как вставить текст перед указанной строкой в ​​файле с помощью sed?

У вас все в порядке с Python?

from __future__ import print_function
import fileinput

show = False

for line in fileinput.input('a.txt'):
  if "START HERE" in line:
    show = True
    continue
  if "END HERE" in line:
    show = False
    try:
      input("Press ENTER to continue")
    except:
      pass
    print(chr(27) + "[2J") # Clear screen
    continue
  if show:
    print(line, end='')
0
21.05.2021, 05:02
1 ответ

Команда i\принимает буквальные жесткие -закодированные символы новой строки, разделенные обратной косой чертой, а не \nИспользуйте следующий шаблон, чтобы внести изменения во входной файл.

sed -i.BAK -e '
/<\/schema>/i\
    <key name='\''enabled'\'' type='\''b'\''>\
      <summary>Enable remote access to the desktop</summary>\
      <description>\
      If true, allows remote access to the desktop via the RFB\
      protocol. Users on remote machines may then connect to the\
      desktop using a VNC viewer.\
      </description>\
      <default>false</default>\
    </key>
' /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml

## now do a diff between these:
diff /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml.BAK
0
28.07.2021, 11:30

Теги

Похожие вопросы