如何使用 shell 从文件中删除多个字符串

如何使用 shell 从文件中删除多个字符串
<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

我需要使用 bash 脚本或我可以在 Linux 系统上运行的任何类型的脚本删除所有包含 use_profile 0 和 use_group 0 的配置文件。我尝试过 awk、grep 和 sed,但没有取得任何进展。

我需要将上面的变成下面的。任何关于使用什么工具的提示、建议,或者直接向我展示如何使用,将不胜感激。

<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

答案1

你的问题非常类似于根据模式匹配打印文件中的整个段落只是在您的情况下,它是由 分隔的文本部分<kernel>...,并且您想要相反的内容(即删除匹配的部分)。您可以根据您的需求调整其中的任何解决方案,例如sed

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

或者,如果您sed不喜欢该语法:

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

答案2

awk方法:

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(文件名)变量包含use_profile_output作为所有不包含零的块<kernel>的结果文件名use_profile0

/<kernel>/- 每次遇到包含关键字的行时,<kernel>将执行后面的大括号组

getline up;- 获取包含关键字的下一条记录use_profile并将其分配给变量up


use_profile_output文件内容(省略了一些行以显示紧凑的输出):

<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

相关内容