当前位置:网站首页>shell编程之sed,正则表达式
shell编程之sed,正则表达式
2022-07-29 09:43:00 【m0_58963135】
1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;
[[email protected] test]# grep -E "^[h|H]" /etc/rc.d/rc.sysinit
2、显示/etc/passwd中以sh结尾的行;
[[email protected] test]# grep sh$ /etc/passwd
3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;
[[email protected] test]# grep "^#[[:space:]]\{1,\}[^[:space:]]" /etc/fstab # /etc/fstab # Created by anaconda on Sat Jul 9 04:36:11 2022 # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file.
4、查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行;
[[email protected] test]# grep "\(to\).*\1" /etc/rc.d/rc.local # In contrast to previous versions due to parallel execution during boot
5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;
[[email protected] test]# grep "\<s[a-Z]*d\>" /etc/inittab # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
6、查找ifconfig命令结果中的1-255之间的整数;
[[email protected] test]# ifconfig | grep -E "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" [[email protected] test]# ifconfig | grep -w "`seq 255`" inet 192.168.223.132 netmask 255.255.255.0 broadcast 192.168.223.255 inet6 fe80::20c:29ff:fe8f:6df2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:8f:6d:f2 txqueuelen 1000 (Ethernet) RX packets 14634 bytes 14380402 (13.7 MiB) TX packets 9060 bytes 724182 (707.2 KiB) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> RX packets 12 bytes 1020 (1020.0 B) TX packets 12 bytes 1020 (1020.0 B)
7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行;
[[email protected] test]# egrep "(Failed|FAILED)" /var/log/secure
8、在/etc/passwd中取出默认shell为bash的行;
[[email protected] test]# grep "bash" /etc/passwd root:x:0:0:root:/root:/bin/bash redhat:x:1000:1000:redhat:/home/redhat:/bin/bash tom:x:1003:1003::/home/tom:/bin/bash user1:x:1004:1004::/home/user1:/bin/bash user2:x:1005:1005::/home/user2:/bin/bash user3:x:1006:1006::/home/user3:/bin/bash user4:x:1007:1007::/home/user4:/bin/bash user5:x:1008:1008::/home/user5:/bin/bash user6:x:1009:1009::/home/user6:/bin/bash user7:x:1010:1010::/home/user7:/bin/bash user8:x:1011:1011::/home/user8:/bin/bash user9:x:1012:1012::/home/user9:/bin/bash user10:x:1013:1013::/home/user10:/bin/bash user11:x:1014:1014::/home/user11:/bin/bash user12:x:1015:1015::/home/user12:/bin/bash user13:x:1016:1016::/home/user13:/bin/bash user14:x:1017:1017::/home/user14:/bin/bash user15:x:1018:1018::/home/user15:/bin/bash user16:x:1019:1019::/home/user16:/bin/bash user17:x:1020:1020::/home/user17:/bin/bash user18:x:1021:1021::/home/user18:/bin/bash user19:x:1022:1022::/home/user19:/bin/bash user20:x:1023:1023::/home/user20:/bin/bash
9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息;
[[email protected] test]# ll /etc/ns*.conf lrwxrwxrwx. 1 root root 29 Jul 9 12:47 /etc/nsswitch.conf -> /etc/authselect/nsswitch.conf
10、高亮显示passwd文件中冒号,及其两侧的字符;
[[email protected] test]# grep -E --color "(.(:.){1,}|.:+.)" /etc/passwd
1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符
[[email protected] etc]# vim testgrub.conf jdskjgfkllksllkldkjfl kjkdjjlas dksjkdjksjl fjkjdsjf kdjfjdskla jdksjfkjksjlkf kfdsjlflmmflmdl dsjfjlsaksakkfldkkdkl [[email protected] etc]# sed -r 's/^[[:space:]]//' /etc/testgrub.conf jdskjgfkllksllkldkjfl kjkdjjlas dksjkdjksjl fjkjdsjf kdjfjdskla jdksjfkjksjlkf kfdsjlflmmflmdl dsjfjlsaksakkfldkkdkl
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[[email protected] rc.d]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Sat Jul 9 04:36:11 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # /dev/mapper/rhel-root / xfs defaults 0 0 UUID=8e1cec7b-3876-430c-a34c-8c6c658ce96a /boot xfs defaults 0 0 /dev/mapper/rhel-swap none swap defaults 0 0 /dev/sr0 /mnt iso9660 defaults 0 0 [[email protected] test]# sed -r 's/^#[[:space:]]*//' /etc/fstab /etc/fstab Created by anaconda on Sat Jul 9 04:36:11 2022 Accessible filesystems, by reference, are maintained under '/dev/disk/'. See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. After editing this file, run 'systemctl daemon-reload' to update systemd units generated from this file. /dev/mapper/rhel-root / xfs defaults 0 0 UUID=8e1cec7b-3876-430c-a34c-8c6c658ce96a /boot xfs defaults 0 0 /dev/mapper/rhel-swap none swap defaults 0 0 /dev/sr0 /mnt iso9660 defaults 0 0
3、在/root/install.log每一行行首增加#号
[[email protected] test]# sed -r 's/^.*$/#&/' /root/install.sh
4、在/etc/fstab文件中不以#开头的行的行首增加#号
[[email protected] test]# sed -r 's/^[^\#]/#&/' /etc/fstab # # /etc/fstab # Created by anaconda on Sat Jul 9 04:36:11 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk/'. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run 'systemctl daemon-reload' to update systemd # units generated from this file. # #/dev/mapper/rhel-root / xfs defaults 0 0 #UUID=8e1cec7b-3876-430c-a34c-8c6c658ce96a /boot xfs defaults 0 0 #/dev/mapper/rhel-swap none swap defaults 0 0 #/dev/sr0 /mnt iso9660 defaults 0 0
5、利用sed 取出ifconfig命令中本机的IPv4地址
[[email protected] test]# ifconfig | sed -n 2p | sed -r "s/.*inet[[:space:]]*//" | sed -r "s/[[:space:]]*netmask.*//" 192.168.223.132
6、关闭本机SELinux的功能
[[email protected] test]# sed -I 's#SELINUX=enforced#SELINUX=disabled#g' [[email protected] test]# getenforce Disabled
7、在/etc/hosts配置文件中添加内容
[[email protected] test]# sed -i '1 i 1234567' /etc/hosts [[email protected] test]# sed -i '$ a qwertyu' /etc/hosts [[email protected] test]# cat /etc/hosts 1234567 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.223.132 www.ceshi.com qwertyu
边栏推荐
猜你喜欢

Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages

最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)

Webassembly 2022 questionnaire results are fresh

WebAssembly 2022 问卷调查结果新鲜出炉

Youboxun, the gold donor of the open atom open source foundation, joined hands with partners to help openharmony break the circle!

PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局

23 postgraduate entrance examination people hold on! The first wave of exam abandonment peak has arrived!

Unity xchart3.0 basic usage quick start

附录2-一些简单的练习

Nucleic acid scanning code registration experience (how to improve the correct character recognition rate of OCR)
随机推荐
Opencv introductory basic learning
Implementation of DFA string recognition based on C language simulation
Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10
Network security (6)
User identity identification and account system practice
《LOL》从代码上来说最难的是哪个英雄?
Appendix 2 – some simple exercises
Youboxun, the gold donor of the open atom open source foundation, joined hands with partners to help openharmony break the circle!
View port occupancy
[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题
Detailed explanation: what is the GPS Beidou time service server?
Logistic regression of machine learning
Div horizontal layout aligned on both sides
系统架构师学习
36. JS动画
怎样查询快递物流筛选出无信息单号删除或者复制
【AAAI】用于交通流预测的基于注意力的时空图卷积网络
NFA determination and DFA minimization based on C language
How to realize the isolation level between MySQL transactions and mvcc
Dynamics 365Online 如何自定义商机关闭窗体