Linux基本命令《五,文本分析2》

本文详细介绍了Linux下sed编辑器的基本用法及实例操作,包括行编辑、查找替换、添加删除等功能,通过具体示例展示了sed的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,sed:行编辑器

sed [options] ‘AddressCommand’ file …
-n: 静默模式,不再默认显示模式空间中的内容
-i: 直接修改原文件
-e SCRIPT -e SCRIPT:可以同时执行多个脚本
-f /PATH/TO/SED_SCRIPT
-r: 表示使用扩展正则表达式

sed:行编辑器Command
d: 删除符合条件的行;
p: 显示符合条件的行;
a \string: 在指定的行后面追加新行,内容为string
\n:可以用于换行
i \string: 在指定的行前面添加新行,内容为string
r FILE: 将指定的文件的内容添加至符合条件的行处
w FILE: 将地址指定的范围内的行另存至指定的文件中;
s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串
g: 行内全局替换
i: 忽略字符大小写
s///: s###, s@@@
(), \1, \2

sed:行编辑器Address
可以没有
给定范围
查找指定行/str/

[root@node01 ~]# cat sort.txt 
nana 12
apple 1
orange 8
[root@node01 ~]# sed "2p" sort.txt 
nana 12
apple 1
apple 1
orange 8
[root@node01 ~]# sed -n "2p" sort.txt 
apple 1
[root@node01 ~]# sed "3d" sort.txt
nana 12
apple 1
[root@node01 ~]# cat sort.txt 
nana 12
apple 1
orange 8
# -i: 直接修改原文件
[root@node01 ~]# sed -i "3d" sort.txt
[root@node01 ~]# cat sort.txt 
nana 12
apple 1
[root@node01 ~]# sed "2i\sxt" sort.txt
nana 12
sxt
apple 1
[root@node01 ~]# sed -i "2a\sxt" sort.txt
[root@node01 ~]# cat sort.txt 
nana 12
sxt
sxt
apple 1
[root@node01 ~]# sed "s/sxt/hello/" sort.txt 
nana 12
hello
hello
apple 1
[root@node01 ~]# sed "/sxt/d" sort.txt 
nana 12
apple 1

二,举例说明

[root@node01 ~]# cp /etc/inittab .
[root@node01 ~]# ls
123.bak  abc  anaconda-ks.cfg  grep.txt  inittab  install.log  install.log.syslog  passwd  profile  sort.txt  wangle  x  y
[root@node01 ~]# cat inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:3:initdefault:
[root@node01 ~]# sed "s/[0-9]/8/" inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(8), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   8 - halt (Do NOT set initdefault to this)
#   8 - Single user mode
#   8 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   8 - Full multiuser mode
#   8 - unused
#   8 - X11
#   8 - reboot (Do NOT set initdefault to this)
# 
id:8:initdefault:

[root@node01 ~]# sed "s/id:3:initdefault:/8/" inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
8

[root@node01 ~]# sed "s/id:3:initdefault:/id8:initdefault:/" inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id8:initdefault:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值