set命令用法详情及演示

本文详细介绍了shell中set命令的功能和用法,主要用于设置shell执行方式和查看/设置shell变量。接着重点讲解了sed命令,包括其常用选项如-n、-e、-f、-r、-i等,以及a、i、c、d、p、s等动作的使用。通过多个实际示例,展示了sed如何进行文本插入、替换、删除等操作,帮助读者深入理解sed在文本处理中的应用。

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

set命令用法详情及演示

一. set命令的介绍及用法

  • 1.set命令用于设置shell,set指令能设置所使用shell的执行方式,可依照不同的需求来做设置
  • 2.set命令作用主要是显示系统中已经存在的shell变量,以及设置shell变量的新变量值。
  • 3.使用set更改shell特性时,符号”+”和”-“的作用分别是打开和关闭指定的模式。

用法

-n : 使用静默模式,在一般的sed的用法中,所有来自stdin(标准输出)的数据一般都会被列出到屏幕上。
     但如果加上-n参数后,则只要经过sed特殊处理的那行才会被列出来。
-e : 直接在命令行模式上进行sed的操作编辑
-f : 直接将sed的操作写在一个文件内,-f filename则可以执行filename内的sed操作
-r : 使用扩展正则表达式的语法
-i : 直接修改文件内容,而不是输出到屏幕上
command说明:[n1][,n2] action
n1,n2 : 一般代表【选择进行操作(action)的行数】,举例:如果我的操作是需要在
        5行到20行之间进行的,则【5,20[action]】。

单行模式空间
a : 新增。a的后面接字符,而这些字符会在新增到下一行
i : 插入。i的后面接字符,而这些字符会在新增到上一行
c : 替换。c的后面接字符,这些字符替换n1到n2的行
d : 删除。因为是删除,所以d后面通常不接任何东西
p : 打印。将匹配的数据打印出来。通常p会与选项-n一起使用
s : 替换。将文件原内容替换为新内容。举例:s/lod/new/g
n : 读取匹配的数据的下一行,覆盖模型空间的前一行(也就是被匹配的行),结果交给下一个参数处理
多行模式空间
N : 读取匹配的数据的下一行追加到模式空间,同时将两行看做一行,但是两行之间依然含有\n换行符
P : 打印。打印模式空间开端至\n(换行)前面的内容,并追加到默认输出之前。
D : 如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本, 并不会读取新的输入行,
    而使用合成的模式空间重新启动循环。如果模式空间 不包含换行符,则会像发出d命令那样启动正常的新循环
    替换标记
g 表示行内全面替换。 
p 表示打印行。 
w 表示把行写入一个文件。 
x 表示互换模板块中的文本和缓冲区中的文本。 
y 表示把一个字符翻译为另外的字符(但是不用于正则表达式)
\\1 子串匹配标记

sed命令演示

//使用sed命令对该文件进行演示
[root@zabbix ~]# cp /etc/sysconfig/selinux aaa
[root@zabbix ~]# cat aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted



#-a参数前面接字符在就是在第及行的下面插入一个任意数
[root@zabbix ~]# sed '1ahello' aaa

hello
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

#用i参数在第五行的上面插入hello
[root@zabbix ~]# sed '5ihello' aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
hello
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.


#参数c把第3行换成lx
[root@zabbix ~]# cat aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
[root@zabbix ~]# sed '3clx' aaa

# This file controls the state of SELinux on the system.
lx
#     enforcing - SELinux security policy is enforced.

#参数d删除匹配到‘SELINUX’的行
[root@zabbix ~]# sed '/SELINUX/d' aaa

# This file controls the state of SELinux on the system.
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.


#单独用s参数替换文件内容只会替换该行匹配到的第一个其他一样的不变如果要全部替换必须要 s和g一起使用
我们修改一下这个aaa文件
添加一行
SELINUX=disabled  disabled
disabled disabled
[root@zabbix ~]# sed 's/disabled/enable/' aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     enable - No SELinux policy is loaded.
SELINUX=enable  disabled
enable disabled

SELINUXTYPE=targeted
***只修改了第一行的第一个其余未修改
[root@zabbix ~]# sed 's/disabled/enable/g' aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     enable - No SELinux policy is loaded.
SELINUX=enable  enable
enable enable
全部修改

#单独使用p参数可以看到不仅打印出匹配‘root’的行,还把原本内容一起打印了出来
[root@zabbix ~]# sed '/disabled/p' aaa

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  disabled
SELINUX=disabled  disabled
disabled disabled
disabled disabled
#通常p参数会搭配-n选项一起使用,这样方便观看和查阅
[root@zabbix ~]# sed -n '/disabled/p' aaa
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  disabled
disabled disabled

#用n参数读取当前行(匹配到的行)的下一行至模式空间,n参数会覆盖模式空间的 \
#前一行(也就是含有'disabled'的行)。此时模式空间只有‘下一行’,后面的d参数会 \
#删除模式空间的内容,所以sed会打印出含有disabled的行
[root@zabbix ~]# sed '/disabled/n;d' aaa
#     disabled - No SELinux policy is loaded.
disabled disabled


#可以发现使用N参数没有内容被打印出来,是因为N参数读取当前行的下一行至模式空间时\
#并不会覆盖模式空间的上一行,此时模式空间‘当前行’与‘下一行’都存在,后面的d参数会\
#模式空间的内容,所有没有内容被打印出来
[root@zabbix ~]# sed '/disabled/N;d' aaa
[root@zabbix ~]# 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值