sed
(1)原理
非交互式的编辑器
流式
read ---》 excute ---》Print ---》 repeat
(2)语法:
格式1:sed 选项 ‘[定址符] 命令’ 文件
格式2:前置命令 | sed 选项 ‘[定址符] 命令
(3)打印
-n 不打印模式空间
sed -n '/^root/,/^apache/ p' /etc/passwd
sed -n 'm,/^apache/ p' /etc/passwd
sed -n 'm,n p' /etc/passwd
sed -n '/^root/,n p' /etc/passwd
sed -n '$ p' /etc/passwd
sed -n 'm~a p' /etc/passwd
a:步长
(4)多点编辑问题
-e
[root@localhost ~]# sed -n -e '/^root/p' -e '/^apache/p' /etc/passwd
[root@localhost ~]# sed -n \
-e '/^root/p'
-e '/^apache/p'
/etc/passwd
root:x:0:0:root:/root:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
{}
[root@localhost ~]# sed -n '{
/^root/ p
/^apache/ p
}' /etc/passwd
-f 文本
[root@localhost test2]# sed -n -f com.txt /etc/passwd
[root@localhost test2]# cat com.txt
/^root/ p
/^apache/ p
(5)删除
d
sed '/^root/ d' /etc/passwd
sed '2,3 d' /etc/passwd
sed '/^root/,3 d' /etc/passwd
sed '/3,/^apache/ d' /etc/passwd
....
sed '/^root/,+3 d' /etc/passwd
sed '/^$/ d' /etc/passwd
sed '/^#/ d' /etc/passwd
替换
sed '/^x/s/old/new/' /etc/passwd
sed '2 s/old/new/' /etc/passwd
sed '2,5 s/old/new/' /etc/passwd
sed '2,5s/old/new/g' /etc/passwd
sed '2,5s/old/new/i' /etc/passwd
sed '2,5s/old/new/w d.txt' /etc/passwd
sed 's/^/ls -l /e' /etc/file =======>ls -l /etc/passwd ;ls -l /etc/shadow
cat file
/etc/passwd
/etc/shadow
后项引用
[root@localhost ~]# sed 's/([^,])./\1/g' test
[root@localhost ~]# sed 's/([^:])./\1/' /etc/passwd (分组)
[root@localhost ~]# echo "This Is a Test.." |sed 's/(\b[A-Z])/(\1)/g'
[root@localhost ~]# cat numbers.txt
[root@localhost ~]# sed 's/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g' numbers.txt
[root@localhost ~]# sed 's/^([^,]),([^,]),([^,]*)/\1,\3/' test
sed 中的正则
用户提权:
(1)su
普通用户的su进制,PAM机制(433)
gpasswd -a username wheel :all root
(2)sudo
三个分组:
用户:
命令:
主机:
test4 ALL=(ALL) /bin/mount /dev/sr0 /media
test4 ALL=(root) /usr/sbin/,!/usr/sbin/userdel
test4 ALL=(root) /bin/cat /var/log/message,!/bin/cat /var/log/message *
User_Alias NETUSER=test5,test6
Cmnd_Alias IP=/sbin/ip
NETUSER ALL=(root) NOPASSWD: IP
(3)LADP 统一身份认证
扩展堡垒机(jumpserver)
awk:
(1)语法
awk [参数] ‘BEGIN{} // {print $ 1} END{}’ file
comm |awk [参数] ‘BEGIN{} // {print $
1} END{}'
(2)处理流程
begin()---一行--处理--重复---end()
(3)变量
系统变量(实例)
自定义变量
(4)练习
文本处理:
vim,vi
sed,awk,grep
cut,cat,tr,uniq,sort,comm
(1)sort
按数字排序
[root@www test]# sort -n file1
逆序排列
[root@www test]# sort -r file1
安月分排列
[root@www test]# sort -M file1
需要合并两个排过序的文件,而且不需要对合并后的文件再次排序
[root@www test]# sort -m sorted1 sorted2
测试文件是否被排过序
[root@www test]# sort -nC file2
指定主建来排序
[root@www test]# sort -k 4 file1
(2)uniq
uniq命令通过消除重复内容,从而给输入中找出单一的行,也可以找出重复行。uniq只能用于拍过序的数据输入,因此,uniq要么使用管道,要么将排序的文件作为输入。
[root@www test]# cat file11
112
112
134
344
123
134
只显示连续唯一的行
[root@www test]# uniq file11
112
134
344
123
134
不显示连续唯一行
[root@www test]# uniq -u file11
134
344
123
134
排序
[root@www test]# sort file11
112
112
123
134
134
344
显示不重复的唯一行
[root@www test]# sort file11 |uniq -u
123
344
显示唯一行
[root@www test]# sort file11 |uniq
112
123
134
344
统计各行在文中出现的次数
[root@www test]# sort file11 |uniq -c
2 112
1 123
2 134
1 344
找出文中的重复行
[root@www test]# sort file11 |uniq -d
112
134
并编号
[root@www test]# sort file11 |uniq -d -c
2 112
2 134
-s 指定可以跳过前n个字符。 -w 指定用于比较的最大字符数
[root@www test]# cat file10
u:01:gnu
d:04:linux
u:01:bash
u:01:hacku:01
[root@www test]# sort file10 |uniq -s 2 -w 2
d:04:linux
u:01:bash
用uniq生成字符串样式
[root@www test]# INPUT="absaabbsshskdk"
[root@www test]# echo $INPUT
absaabbsshskdk
[root@www test]# echo $INPUT |sed 's/[^\n]/&\n/g'
a
b
s
a
a
b
b
s
s
h
s
k
d
k
[root@www test]# echo /d'
a
b
s
a
a
b
b
s
s
h
s
k
d
k
[root@www test]# echo $ INPUT |sed 's/[^\n]/&\n/g'|sed '/^ $ /d'|sort
a
a
a
b
b
b
d
h
k
k
s
s
s
s
[root@www test]# echo $ INPUT |sed 's/[^\n]/&\n/g'|sed '/^ $ /d'|sort|uniq
a
b
d
h
k
s
[root@www test]# echo $ INPUT |sed 's/[^\n]/&\n/g'|sed '/^ $/d'|sort|uniq -c
3 a
3 b
1 d
1 h
2 k
4 s
[root@www test]# echo $ INPUT |sed 's/[^\n]/&\n/g'|sed '/^ $ /d'|sort|uniq -c |tr -d ' \n'
3a3b1d1h2k4s[root@www test]#
交集:打印两个文本所共有的行。
求差:打印指定文件所包含的且不相同的那些行。
差集:打印出包含在文件A 中,但不包含在其他指定文件中的那些行。
[root@www files]# cat file1
apple
orange
gold
silver
steel
iron
[root@www files]# cat file2
orange
gold
cookies
carrot
[root@www files]# sort file1 -o file1;sort file2 -o file2
[root@www files]# comm file1 file2
apple
carrot
cookies
gold
iron
orange
silver
steel
输出的第一列为只有在file1中出现的行,第二列为只有在file2中出现的行,第三列为file1和file2***有的行。
[root@www files]# comm file1 file2 -1 -2
gold
orange
[root@www files]# comm file1 file2 -1
carrot
cookies
gold
orange
[root@www files]# comm file1 file2 -1 -3
carrot
cookies
-1 从输出中删除第列
-2
-3