Text-Processing

如何使用 shell 從文件中刪除多個字元串

  • May 10, 2017
<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

您的問題與在模式匹配的文件中列印整個段落非常相似,只是在您的情況下,它是由分隔的文本部分<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

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_profile不帶零0

/<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

引用自:https://unix.stackexchange.com/questions/364165