sed第三天
利用sed 取出ifconfIg ens33命令中本机的IPv4地址 可以百度扩展 了解即可 也可以用别的命令实现 只要有结果也可以
ifconfig ens33 | sed -n 's/.*inet \([0-9\.]\+\) .*/\1/p'
ip a | grep -A2 'ens33:' |tail -n1 |awk '{print $2}'
[root@master ~]# ip a | grep -A2 'eno16777736:' |tail -n1 |awk '{print $2}'
10.0.0.164/24
[root@master ~]# ifconfig eno16777736 | sed -n 's/.*inet \([0-9\.]\+\) .*/\1/p'
10.0.0.164
这两个命令都是用来从Linux系统的网络接口配置信息中提取出ens33网卡的IPv4地址。
1. `ifconfig ens33 | sed -n 's/.*inet \([0-9\.]\+\) .*/\1/p'`
- `ifconfig ens33`: 这个命令显示名为ens33的网络接口详细信息。在较旧版本的Linux系统中,`ifconfig`是用于配置和查看网络接口状态的标准命令。
- 输出通过管道(`|`)传递给`sed`命令:
- `-n` 参数表示不打印输入文件中的任何行,除非特别指定。
- `s/.*inet \([0-9\.]\+\) .*/\1/p` 是一个替换模式,它查找匹配以下模式的行:
- `.*inet` 匹配任何字符(`.`),直到遇到 "inet";
- `\([0-9\.]\+\)` 是一个捕获组,匹配一串数字和点(即IP地址);
- `.*` 再次匹配任何字符,直到行尾。
- `\1` 在替换字符串中引用了第一个捕获组的内容,也就是IP地址。
- `/p` 表示打印经过替换后与模式匹配的那一部分。
结果:该命令会找到ens33接口下的IPv4地址,并只打印出来。
2. `ip a | grep -A2 'ens33:' | tail -n1 | awk '{print $2}'`
- `ip a` 或者 `ip address` 命令在现代Linux发行版中广泛使用,用于显示所有网络接口及其配置详情。
- `grep -A2 'ens33:'` 从输出中查找包含 "ens33:" 的行,并显示其后两行内容(因为IP地址通常紧跟在描述行之后的第二行)。
- `tail -n1` 只选择最后显示的一行,即包含IP地址的那一行。
- `awk '{print $2}'` 使用awk命令来处理文本,这里选择并打印每行的第二列数据($2),在本例中就是IPv4地址。
结果:这个命令同样是从输出中定位到ens33接口的IPv4地址并将其打印出来。
把/etc/passwd 复制到/root/test.txt,用sed打印所有行
cp /etc/passwd /root/test.txt
sed -n 'p' /root/test.txt
或者
cat /root/test.txt
[root@master ~]# cp /etc/passwd /root/test.txt
cp: overwrite ‘/root/test.txt’? y
[root@master ~]# sed -n 'p' /root/test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it101:x:1001:1001::/home/it101:/bin/bash
it1:x:1002:1002::/home/it1:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
oldboy01:x:1102:1103::/home/oldboy01:/bin/bash
oldboy02:x:1103:1104::/home/oldboy02:/bin/bash
oldboy03:x:1104:1105::/home/oldboy03:/bin/bash
oldboy04:x:1105:1106::/home/oldboy04:/bin/bash
oldboy05:x:1106:1107::/home/oldboy05:/bin/bash
oldboy06:x:1107:1108::/home/oldboy06:/bin/bash
oldboy07:x:1108:1109::/home/oldboy07:/bin/bash
oldboy08:x:1109:1110::/home/oldboy08:/bin/bash
oldboy09:x:1110:1111::/home/oldboy09:/bin/bash
oldboy10:x:1111:1112::/home/oldboy10:/bin/bash
mailuser:x:1112:1113::/home/mailuser:/bin/bash
oldboy:x:1113:1114::/home/oldboy:/bin/bash
[root@master ~]# cat /root/test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it101:x:1001:1001::/home/it101:/bin/bash
it1:x:1002:1002::/home/it1:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
oldboy01:x:1102:1103::/home/oldboy01:/bin/bash
oldboy02:x:1103:1104::/home/oldboy02:/bin/bash
oldboy03:x:1104:1105::/home/oldboy03:/bin/bash
oldboy04:x:1105:1106::/home/oldboy04:/bin/bash
oldboy05:x:1106:1107::/home/oldboy05:/bin/bash
oldboy06:x:1107:1108::/home/oldboy06:/bin/bash
oldboy07:x:1108:1109::/home/oldboy07:/bin/bash
oldboy08:x:1109:1110::/home/oldboy08:/bin/bash
oldboy09:x:1110:1111::/home/oldboy09:/bin/bash
oldboy10:x:1111:1112::/home/oldboy10:/bin/bash
mailuser:x:1112:1113::/home/mailuser:/bin/bash
oldboy:x:1113:1114::/home/oldboy:/bin/bash
打印test.txt的3到10行
sed -n '3,10p' /root/test.txt
head -n 10 /root/test.txt | tail -n +3
1. `sed -n '3,10p' /root/test.txt`
- `sed` 是流编辑器,可以对输入流(如文件或管道的输出)进行各种文本处理操作。
- `-n` 参数表示默认情况下不打印任何行,除非使用了特定的打印命令。
- `'3,10p'` 是一个地址范围和命令。在这个范围内(即从第3行到第10行),执行打印命令`p`,所以只打印这些行的内容。
结果:此命令会打印出`/root/test.txt`文件中的第3至第10行。
2. `head -n 10 /root/test.txt | tail -n +3`
- `head -n 10 /root/test.txt` 命令首先取出文件的前10行。
- `tail -n +3` 接收前面命令的输出,并从中选择从第3行开始的所有行(这里的`+3`表示从第3行开始,包括第3行)。
结果:这个命令组合同样会打印出`/root/test.txt`文件中的第3至第10行。
虽然两个命令实现的目标相同,但方式不同。`sed`命令是直接针对文件内容进行定位和操作;而`head`与`tail`结合的方式则是先获取一定数量的行,再从中筛选需要的部分。
[root@master ~]# sed -n '3,10p' /root/test.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@master ~]# head -n 10 /root/test.txt |tail -n +3
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
打印test.txt 中包含 ‘root’ 的行
grep 'root' /root/test.txt
[root@master ~]# grep 'root' /root/test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
删除test.txt 的15行以及以后所有行
sed '15,$d' /root/test.txt
[root@master ~]# sed '15,$d' /root/test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
删除test.txt中包含 ‘bash’ 的行
sed '/bash/d' /root/test.txt
[root@master ~]# sed '/bash/d' /root/test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
替换test.txt 中 ‘root’ 为 ‘ha’
sed 's/root/hs/g' /root/test.txt
[root@master ~]# sed 's/root/hs/g' /root/test.txt
hs:x:0:0:hs:/hs:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/hs:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it101:x:1001:1001::/home/it101:/bin/bash
it1:x:1002:1002::/home/it1:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
oldboy01:x:1102:1103::/home/oldboy01:/bin/bash
oldboy02:x:1103:1104::/home/oldboy02:/bin/bash
oldboy03:x:1104:1105::/home/oldboy03:/bin/bash
oldboy04:x:1105:1106::/home/oldboy04:/bin/bash
oldboy05:x:1106:1107::/home/oldboy05:/bin/bash
oldboy06:x:1107:1108::/home/oldboy06:/bin/bash
oldboy07:x:1108:1109::/home/oldboy07:/bin/bash
oldboy08:x:1109:1110::/home/oldboy08:/bin/bash
oldboy09:x:1110:1111::/home/oldboy09:/bin/bash
oldboy10:x:1111:1112::/home/oldboy10:/bin/bash
mailuser:x:1112:1113::/home/mailuser:/bin/bash
oldboy:x:1113:1114::/home/oldboy:/bin/bash
替换test.txt中 ‘/sbin/nologin’ 为 ‘/bin/login’
sed 's#/sbin/nologin#/bin/login#g' /root/test.txt
格式为 `s/原字符串/新字符串/flags`
命令解析:
- `sed`:流编辑器,它能够处理输入流(如文件、管道输出)并执行一系列编辑操作。
- `'s#/sbin/nologin#/bin/login#g'` 是一个`s`替换命令,格式为`s/原字符串/新字符串/flags`。
- `#` 在这里用作分隔符,因为待替换的字符串中包含斜线 `/`,为了避免混淆,我们可以选择不常用的字符作为分隔符。默认情况下,`sed`使用斜线(`/`)作为分隔符,但此处以井号(`#`)替代。
- `s` 后面跟着两个模式,第一个是 `#sbin/nologin#`,表示查找并替换任何出现的 `/sbin/nologin` 字符串;
- 第二个是 `#/bin/login#`,这是要替换成的新字符串,即把找到的 `/sbin/nologin` 替换为 `/bin/login`。
- `g` 是一个标志(flag),表示全局(global)替换,在一行中如果有多处匹配则全部替换,而不是只替换第一个匹配项。
- `/root/test.txt` 是指定的输入文件,`sed`会读取这个文件的内容,并按照给出的替换规则进行操作。
[root@master ~]# sed 's#/sbin/nologin#/bin/login#g' /root/test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/login
daemon:x:2:2:daemon:/sbin:/bin/login
adm:x:3:4:adm:/var/adm:/bin/login
lp:x:4:7:lp:/var/spool/lpd:/bin/login
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/bin/login
operator:x:11:0:operator:/root:/bin/login
games:x:12:100:games:/usr/games:/bin/login
ftp:x:14:50:FTP User:/var/ftp:/bin/login
nobody:x:99:99:Nobody:/:/bin/login
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/bin/login
dbus:x:81:81:System message bus:/:/bin/login
polkitd:x:999:998:User for polkitd:/:/bin/login
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/login
postfix:x:89:89::/var/spool/postfix:/bin/login
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/login
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/bin/login
it101:x:1001:1001::/home/it101:/bin/bash
it1:x:1002:1002::/home/it1:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/bin/login
oldboy01:x:1102:1103::/home/oldboy01:/bin/bash
oldboy02:x:1103:1104::/home/oldboy02:/bin/bash
oldboy03:x:1104:1105::/home/oldboy03:/bin/bash
oldboy04:x:1105:1106::/home/oldboy04:/bin/bash
oldboy05:x:1106:1107::/home/oldboy05:/bin/bash
oldboy06:x:1107:1108::/home/oldboy06:/bin/bash
oldboy07:x:1108:1109::/home/oldboy07:/bin/bash
oldboy08:x:1109:1110::/home/oldboy08:/bin/bash
oldboy09:x:1110:1111::/home/oldboy09:/bin/bash
oldboy10:x:1111:1112::/home/oldboy10:/bin/bash
mailuser:x:1112:1113::/home/mailuser:/bin/bash
oldboy:x:1113:1114::/home/oldboy:/bin/bash
打印/etc/passwd的奇数行?
sed 'n;d' /etc/passwd
- `sed` 是一个流编辑器,用于逐行处理输入文本。
- `'n;d'` 是一组命令:
- `n` 命令会读取下一行并打印当前行(默认情况下,sed会打印每一行)。
- `d` 命令表示删除当前行,即不打印当前行。
因此,当执行到第一行时,它会被打印出来(因为`n`命令会打印当前行),然后跳过打印下一行(因为它被删除了)。接着来到第三行,同样打印并跳过第四行,如此反复,结果就是只打印了奇数行的内容。
[root@master ~]# sed 'n;d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it1:x:1002:1002::/home/it1:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
oldboy02:x:1103:1104::/home/oldboy02:/bin/bash
oldboy04:x:1105:1106::/home/oldboy04:/bin/bash
oldboy06:x:1107:1108::/home/oldboy06:/bin/bash
oldboy08:x:1109:1110::/home/oldboy08:/bin/bash
oldboy10:x:1111:1112::/home/oldboy10:/bin/bash
oldboy:x:1113:1114::/home/oldboy:/bin/bash
把/etc/httpd/conf/httpd.conf?件内的Linsten 80改为Listen 8081 sed 完成
sed -i 's/Listen 80/Listen 8081/g' /etc/httpd/conf/httpd.conf