Como remover várias strings de um arquivo usando o shell

0
<kernel>
use_profile 0
use_group 0

file execute /lib64/rc/sh/cgroup-release-agent.sh exec.realpath="/lib64/rc/sh/cgroup-release-agent.sh" exec.argv[0]="/lib64/rc/sh/cgroup-release-agent.sh"
file execute /sbin/init exec.realpath="/sbin/init" exec.argv[0]="/sbin/init"
file execute /sbin/modprobe exec.realpath="/bin/kmod" exec.argv[0]="/sbin/modprobe"

<kernel> /sbin/init
use_profile 0
use_group 0


<kernel> /sbin/init /sbin/openrc
use_profile 0
use_group 0


<kernel> /sbin/init /sbin/openrc /lib64/rc/sh/init-early.sh
use_profile 0
use_group 0

<kernel> /etc/init.d/dmesg
use_profile 0
use_group 0

file execute /lib64/rc/sh/openrc-run.sh exec.realpath="/lib64/rc/sh/openrc-run.sh" exec.argv[0]="/lib64/rc/sh/openrc-run.sh"
file read /etc/rc.conf
file read /run/openrc/depconfig
file read /run/openrc/deptree
file read /sbin/openrc-run
file read proc:/cmdline
file symlink /run/openrc/started/dmesg symlink.target="/etc/init.d/dmesg"
file symlink /run/openrc/starting/dmesg symlink.target="/etc/init.d/dmesg"
file unlink /run/openrc/exclusive/dmesg
file unlink /run/openrc/starting/dmesg
misc env CONFIG_PROTECT
misc env CONFIG_PROTECT_MASK
misc env DCC_EMAILLOG_WHOM_TO_BLAME
misc env DISTCC_ENABLE_DISCREPANCY_EMAIL
misc env DISTCC_FALLBACK
misc env DISTCC_SAVE_TEMPS
misc env DISTCC_SSH
misc env DISTCC_TCP_CORK
misc env DISTCC_VERBOSE
misc env EINFO_LOG
misc env GCC_SPECS
misc env GSETTINGS_BACKEND
misc env GUILE_LOAD_PATH
misc env INFOPATH
misc env LANG
misc env LESS
misc env LESSOPEN
misc env MANPAGER
misc env MANPATH
misc env OPENCL_PROFILE
misc env OPENGL_PROFILE
misc env PATH
misc env QT_GRAPHICSSYSTEM
misc env RC_BOOTLEVEL
misc env RC_DEFAULTLEVEL
misc env RC_LIBEXECDIR
misc env RC_PID
misc env RC_RUNLEVEL
misc env RC_SVCDIR
misc env RC_TMPDIR
misc env RC_UNAME
misc env RC_VERSION
misc env ROOTPATH
misc env TERM
misc env UNCACHED_ERR_FD
misc env VBOX_APP_HOME
misc env XDG_CONFIG_DIRS
misc env XDG_DATA_DIRS
misc env XSESSION


<kernel> /usr/sbin/ntpdate
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
misc env DISPLAY
misc env LC_CTYPE
misc env TERM
misc env XAUTHORITY
misc env PATH
misc env COLORTERM
misc env MAIL
misc env LOGNAME
misc env USER
misc env USERNAME
misc env HOME
misc env SHELL
misc env SUDO_COMMAND
misc env SUDO_USER
misc env SUDO_UID
misc env SUDO_GID
network unix dgram send /dev/log
file read /etc/nsswitch.conf
file read /etc/services
file read /etc/host.conf
file read /etc/resolv.conf
file read /etc/hosts
network inet dgram send 127.0.0.1 53
file ioctl socket:[family=2:type=2:protocol=17] 0x541B
file read /etc/gai.conf
network inet dgram bind/send :: 123
network inet dgram bind/send 0.0.0.0 123



<kernel> /sbin/init /sbin/openrc /lib64/rc/sh/init-early.sh /usr/bin/loadkeys
use_profile 0
use_group 0


<kernel> /sbin/init /sbin/openrc /lib64/rc/sh/init.sh
use_profile 0
use_group 0


<kernel> /etc/init.d/ntpd
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
misc env DISPLAY
misc env LC_CTYPE
misc env TERM
misc env XAUTHORITY
misc env PATH
misc env COLORTERM
misc env MAIL
misc env LOGNAME
misc env USER
misc env USERNAME
misc env HOME
misc env SHELL
misc env SUDO_COMMAND
misc env SUDO_USER
misc env SUDO_UID
misc env SUDO_GID
file read /sbin/openrc-run
file read /etc/rc.conf
file read proc:/cmdline
file read /etc/profile.env
file read proc:/cpuinfo
file read proc:/1/environ
file read /run/openrc/softlevel
file create /run/openrc/exclusive/ntpd 0664
file write/unlink /run/openrc/exclusive/ntpd
file symlink /run/openrc/starting/ntpd symlink.target="/etc/init.d/ntpd"
file read /run/openrc/depconfig
file read /run/openrc/deptree
file execute /lib64/rc/sh/openrc-run.sh exec.realpath="/lib64/rc/sh/openrc-run.sh" exec.argv[0]="/lib64/rc/sh/openrc-run.sh"
misc env CONFIG_PROTECT
misc env CONFIG_PROTECT_MASK
misc env GCC_SPECS
misc env GSETTINGS_BACKEND
misc env INFOPATH
misc env LESS
misc env LESSOPEN
misc env MANPAGER
misc env MANPATH
misc env OPENGL_PROFILE
misc env QT_GRAPHICSSYSTEM
misc env ROOTPATH
misc env VBOX_APP_HOME
misc env XDG_CONFIG_DIRS
misc env XDG_DATA_DIRS
misc env RC_VERSION
misc env RC_LIBEXECDIR
misc env RC_SVCDIR
misc env RC_TMPDIR
misc env RC_BOOTLEVEL
misc env RC_RUNLEVEL
misc env RC_DEFAULTLEVEL
misc env RC_UNAME
misc env EINFO_LOG
misc env RC_SVCNAME
misc env RC_OPENRC_PID
misc env RC_RUNSCRIPT_PID
misc env RC_CMD
file read /bin/bash
file read/write /dev/tty
file read /usr/lib64/locale/locale-archive
file read /etc/nsswitch.conf
file read /etc/passwd
file ioctl devpts:/3 0x540F
file read /lib64/rc/sh/openrc-run.sh
file read /lib64/rc/sh/functions.sh
file execute /lib64/rc/bin/eval_ecolors exec.realpath="/lib64/rc/bin/eval_ecolors" exec.argv[0]="eval_ecolors"
misc env PWD
misc env SHLVL
misc env _
file read /usr/share/terminfo/r/rxvt-unicode-256color
file ioctl devpts:/3 0x5413
file read /lib64/rc/sh/rc-functions.sh
file read /lib64/rc/sh/rc-cgroup.sh
file read /etc/conf.d/ntpd
file read /lib64/rc/sh/runit.sh
file read /lib64/rc/sh/s6.sh
file read /lib64/rc/sh/start-stop-daemon.sh
file read /lib64/rc/sh/supervise-daemon.sh
file write/truncate /sys/fs/cgroup/cpu/tasks
file write/truncate /sys/fs/cgroup/cpuacct/tasks
file write/truncate /sys/fs/cgroup/cpuset/tasks
file write/truncate /sys/fs/cgroup/freezer/tasks
file write/truncate /sys/fs/cgroup/memory/tasks
file write/truncate /sys/fs/cgroup/openrc/tasks
file execute /bin/mkdir exec.realpath="/bin/mkdir" exec.argv[0]="mkdir"
misc env RC_SERVICE
misc env SVCNAME
file mkdir /sys/fs/cgroup/openrc/ntpd/ 0700
file write/truncate /sys/fs/cgroup/openrc/ntpd/tasks
file read /etc/init.d/ntpd
file execute /lib64/rc/bin/ebegin exec.realpath="/lib64/rc/bin/ebegin" exec.argv[0]="ebegin"
file execute /sbin/start-stop-daemon exec.realpath="/sbin/start-stop-daemon" exec.argv[0]="start-stop-daemon"
file read/write /dev/null
file ioctl /dev/tty 0x5422
file execute /usr/sbin/ntpd exec.realpath="/usr/sbin/ntpd" exec.argv[0]="/usr/sbin/ntpd"
file read /usr/lib64/gcc/x86_64-pc-linux-gnu/6.3.0/libgcc_s.so.1
network unix dgram send /dev/log
file mkdir /run/openrc/daemons/ntpd/ 0755
file create /run/openrc/daemons/ntpd/001 0666
file read/write/unlink /run/openrc/daemons/ntpd/001
file execute /lib64/rc/bin/eend exec.realpath="/lib64/rc/bin/eend" exec.argv[0]="eend"
file execute /lib64/rc/bin/service_set_value exec.realpath="/lib64/rc/bin/service_set_value" exec.argv[0]="service_set_value"
file read /etc/services
file read /etc/host.conf
file read /etc/resolv.conf
file read /etc/hosts
file create /run/ntpd.pid 0666
file read/write/unlink /run/ntpd.pid
file mkdir /run/openrc/options/ntpd/ 0755
file read /etc/ntp.conf
file create /run/openrc/options/ntpd/command 0666
file read/write/unlink /run/openrc/options/ntpd/command
file ioctl socket:[family=1:type=2:protocol=0] 0x8933
file create /run/openrc/options/ntpd/pidfile 0666
file read/write/unlink /run/openrc/options/ntpd/pidfile
file symlink /run/openrc/started/ntpd symlink.target="/etc/init.d/ntpd"
file unlink /run/openrc/starting/ntpd
file symlink /run/openrc/stopping/ntpd symlink.target="/etc/init.d/ntpd"
file unlink /run/openrc/started/ntpd
file execute /lib64/rc/bin/service_get_value exec.realpath="/lib64/rc/bin/service_get_value" exec.argv[0]="service_get_value"
file unlink /run/openrc/stopping/ntpd
file rmdir /run/openrc/options/ntpd/
file rmdir /run/openrc/daemons/ntpd/
network inet dgram bind/send @ALLIPV6 123
network inet dgram bind/send @ALLIPV4 123
file ioctl socket:[family=2:type=2:protocol=17] 0x541B
file read /etc/gai.conf
network inet dgram send 127.0.0.1 53
misc env *
misc env \*
file ioctl /dev/console 0x540F
file ioctl /dev/console 0x5413
file mkdir /sys/fs/cgroup/openrc/ntpd/ 0755
file read/write /dev/console
file read /etc/terminfo/l/linux

Eu preciso remover todos os perfis com use_profile 0 e use_group 0 usando um script bash ou qualquer tipo de script que eu possa rodar em um sistema linux. Eu brinquei com o awk, grep e sed, mas não fiz nenhum progresso.

Eu preciso mudar o acima para o abaixo. Alguma dica, conselhos sobre quais ferramentas usar ou me mostrar como seria muito apreciado.

<kernel> /usr/sbin/ntpdate
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
misc env DISPLAY
misc env LC_CTYPE
misc env TERM
misc env XAUTHORITY
misc env PATH
misc env COLORTERM
misc env MAIL
misc env LOGNAME
misc env USER
misc env USERNAME
misc env HOME
misc env SHELL
misc env SUDO_COMMAND
misc env SUDO_USER
misc env SUDO_UID
misc env SUDO_GID
network unix dgram send /dev/log
file read /etc/nsswitch.conf
file read /etc/services
file read /etc/host.conf
file read /etc/resolv.conf
file read /etc/hosts
network inet dgram send 127.0.0.1 53
file ioctl socket:[family=2:type=2:protocol=17] 0x541B
file read /etc/gai.conf
network inet dgram bind/send :: 123
network inet dgram bind/send 0.0.0.0 123

<kernel> /etc/init.d/ntpd
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
misc env DISPLAY
misc env LC_CTYPE
misc env TERM
misc env XAUTHORITY
misc env PATH
misc env COLORTERM
misc env MAIL
misc env LOGNAME
misc env USER
misc env USERNAME
misc env HOME
misc env SHELL
misc env SUDO_COMMAND
misc env SUDO_USER
misc env SUDO_UID
misc env SUDO_GID
file read /sbin/openrc-run
file read /etc/rc.conf
file read proc:/cmdline
file read /etc/profile.env
file read proc:/cpuinfo
file read proc:/1/environ
file read /run/openrc/softlevel
file create /run/openrc/exclusive/ntpd 0664
file write/unlink /run/openrc/exclusive/ntpd
file symlink /run/openrc/starting/ntpd symlink.target="/etc/init.d/ntpd"
file read /run/openrc/depconfig
file read /run/openrc/deptree
file execute /lib64/rc/sh/openrc-run.sh exec.realpath="/lib64/rc/sh/openrc-run.sh" exec.argv[0]="/lib64/rc/sh/openrc-run.sh"
misc env CONFIG_PROTECT
misc env CONFIG_PROTECT_MASK
misc env GCC_SPECS
misc env GSETTINGS_BACKEND
misc env INFOPATH
misc env LESS
misc env LESSOPEN
misc env MANPAGER
misc env MANPATH
misc env OPENGL_PROFILE
misc env QT_GRAPHICSSYSTEM
misc env ROOTPATH
misc env VBOX_APP_HOME
misc env XDG_CONFIG_DIRS
misc env XDG_DATA_DIRS
misc env RC_VERSION
misc env RC_LIBEXECDIR
misc env RC_SVCDIR
misc env RC_TMPDIR
misc env RC_BOOTLEVEL
misc env RC_RUNLEVEL
misc env RC_DEFAULTLEVEL
misc env RC_UNAME
misc env EINFO_LOG
misc env RC_SVCNAME
misc env RC_OPENRC_PID
misc env RC_RUNSCRIPT_PID
misc env RC_CMD
file read /bin/bash
file read/write /dev/tty
file read /usr/lib64/locale/locale-archive
file read /etc/nsswitch.conf
file read /etc/passwd
file ioctl devpts:/3 0x540F
file read /lib64/rc/sh/openrc-run.sh
file read /lib64/rc/sh/functions.sh
file execute /lib64/rc/bin/eval_ecolors exec.realpath="/lib64/rc/bin/eval_ecolors" exec.argv[0]="eval_ecolors"
misc env PWD
misc env SHLVL
misc env _
file read /usr/share/terminfo/r/rxvt-unicode-256color
file ioctl devpts:/3 0x5413
file read /lib64/rc/sh/rc-functions.sh
file read /lib64/rc/sh/rc-cgroup.sh
file read /etc/conf.d/ntpd
file read /lib64/rc/sh/runit.sh
file read /lib64/rc/sh/s6.sh
file read /lib64/rc/sh/start-stop-daemon.sh
file read /lib64/rc/sh/supervise-daemon.sh
file write/truncate /sys/fs/cgroup/cpu/tasks
file write/truncate /sys/fs/cgroup/cpuacct/tasks
file write/truncate /sys/fs/cgroup/cpuset/tasks
file write/truncate /sys/fs/cgroup/freezer/tasks
file write/truncate /sys/fs/cgroup/memory/tasks
file write/truncate /sys/fs/cgroup/openrc/tasks
file execute /bin/mkdir exec.realpath="/bin/mkdir" exec.argv[0]="mkdir"
misc env RC_SERVICE
misc env SVCNAME
file mkdir /sys/fs/cgroup/openrc/ntpd/ 0700
file write/truncate /sys/fs/cgroup/openrc/ntpd/tasks
file read /etc/init.d/ntpd
file execute /lib64/rc/bin/ebegin exec.realpath="/lib64/rc/bin/ebegin" exec.argv[0]="ebegin"
file execute /sbin/start-stop-daemon exec.realpath="/sbin/start-stop-daemon" exec.argv[0]="start-stop-daemon"
file read/write /dev/null
file ioctl /dev/tty 0x5422
file execute /usr/sbin/ntpd exec.realpath="/usr/sbin/ntpd" exec.argv[0]="/usr/sbin/ntpd"
file read /usr/lib64/gcc/x86_64-pc-linux-gnu/6.3.0/libgcc_s.so.1
network unix dgram send /dev/log
file mkdir /run/openrc/daemons/ntpd/ 0755
file create /run/openrc/daemons/ntpd/001 0666
file read/write/unlink /run/openrc/daemons/ntpd/001
file execute /lib64/rc/bin/eend exec.realpath="/lib64/rc/bin/eend" exec.argv[0]="eend"
file execute /lib64/rc/bin/service_set_value exec.realpath="/lib64/rc/bin/service_set_value" exec.argv[0]="service_set_value"
file read /etc/services
file read /etc/host.conf
file read /etc/resolv.conf
file read /etc/hosts
file create /run/ntpd.pid 0666
file read/write/unlink /run/ntpd.pid
file mkdir /run/openrc/options/ntpd/ 0755
file read /etc/ntp.conf
file create /run/openrc/options/ntpd/command 0666
file read/write/unlink /run/openrc/options/ntpd/command
file ioctl socket:[family=1:type=2:protocol=0] 0x8933
file create /run/openrc/options/ntpd/pidfile 0666
file read/write/unlink /run/openrc/options/ntpd/pidfile
file symlink /run/openrc/started/ntpd symlink.target="/etc/init.d/ntpd"
file unlink /run/openrc/starting/ntpd
file symlink /run/openrc/stopping/ntpd symlink.target="/etc/init.d/ntpd"
file unlink /run/openrc/started/ntpd
file execute /lib64/rc/bin/service_get_value exec.realpath="/lib64/rc/bin/service_get_value" exec.argv[0]="service_get_value"
file unlink /run/openrc/stopping/ntpd
file rmdir /run/openrc/options/ntpd/
file rmdir /run/openrc/daemons/ntpd/
network inet dgram bind/send @ALLIPV6 123
network inet dgram bind/send @ALLIPV4 123
file ioctl socket:[family=2:type=2:protocol=17] 0x541B
file read /etc/gai.conf
network inet dgram send 127.0.0.1 53
misc env *
misc env \*
file ioctl /dev/console 0x540F
file ioctl /dev/console 0x5413
file mkdir /sys/fs/cgroup/openrc/ntpd/ 0755
file read/write /dev/console
file read /etc/terminfo/l/linux
    
por abitofabyte 10.05.2017 / 15:54

2 respostas

2

A sua pergunta é muito parecida com Imprimir o parágrafo inteiro em um arquivo na correspondência de padrões apenas que no seu caso são seções de texto delimitado por <kernel>... e você quer o oposto (ou seja, excluir a seção que corresponde). Você pode adaptar qualquer uma das soluções às suas necessidades, por exemplo com sed :

sed '/<kernel>/!{H;$!d;};x;/\nuse_profile 0\nuse_group 0\n/d' infile

ou, se o seu sed não gostar dessa sintaxe:

sed -e '/<kernel>/!{H;$!d' -e '}' -e 'x;/\nuse_profile 0\nuse_group 0\n/d' infile
    
por 10.05.2017 / 16:37
0

awk abordagem:

awk 'BEGIN{fn="use_profile_output";}/<kernel>/{getline up; if(up~/use_profile [1-9][0-9]*/)
     {f=1; print $0 RS up > fn; next }else{ f=0 }}{ if(f) print > fn; }' file

fn="use_profile_output" - fn (nome do arquivo) variável contendo use_profile_output como o nome do arquivo resultante para todos os blocos <kernel> contendo use_profile sem seguir zero 0

/<kernel>/ - em cada encontro com uma linha contendo <kernel> palavra-chave, o conjunto de chaves seguido será executado

getline up; - obtém o próximo registro contendo use_profile palavra-chave e o atribui à variável up

use_profile_output conteúdo do arquivo (algumas linhas foram omitidas para mostrar a saída compacta):

<kernel> /usr/sbin/ntpdate
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
misc env DISPLAY
misc env LC_CTYPE
...
...
network inet dgram bind/send :: 123
network inet dgram bind/send 0.0.0.0 123



<kernel> /etc/init.d/ntpd
use_profile 3
use_group 0

misc env LS_COLORS
misc env LANG
...
...
misc env *
misc env \*
file ioctl /dev/console 0x540F
file ioctl /dev/console 0x5413
file mkdir /sys/fs/cgroup/openrc/ntpd/ 0755
file read/write /dev/console
file read /etc/terminfo/l/linux
    
por 10.05.2017 / 16:38