624 lines
23 KiB
Markdown
624 lines
23 KiB
Markdown
|
<h1><center>shell三剑客</h1></center>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:非交互式编辑器Sed
|
|||
|
|
|||
|
#### 1.sed介绍
|
|||
|
|
|||
|
![image-20230408155357663](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230408155357663.png)
|
|||
|
|
|||
|
sed 是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出;Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等
|
|||
|
|
|||
|
#### 2.语法格式
|
|||
|
|
|||
|
```shell
|
|||
|
sed [options] 'command' in_file[s]
|
|||
|
```
|
|||
|
|
|||
|
options部分
|
|||
|
|
|||
|
```
|
|||
|
-n 静默输出(不打印默认输出) sed -n '1p' a.txt 想显示第几行就显示第几行
|
|||
|
-e 给予sed多个命令的时候需要-e选项
|
|||
|
#sed -e 's/root/haha/g' -e 's/bash/wwwww/g' passwd > passwd.bak
|
|||
|
如果不用-e选项也可以用分号“;”把多个命令隔开。
|
|||
|
#sed 's/haha/ro/g ; s/wwwww/kkkk/g' passwd | less 这个是-e的结果
|
|||
|
-i -i后面没有扩展名的话直接修改文件,如果有扩展名备份源文件,产生以扩展名结尾的新文件
|
|||
|
#sed -iback1 -e 's/root/rottt/g' -e 's/bash/wwwww/g' passwd //选项-i后面没有空格
|
|||
|
[root@localhost 桌面]# ls
|
|||
|
manifest.txt passwdback1
|
|||
|
-f 当有多个要编辑的项目时,可以将编辑命令放进一个脚本里,再使用sed搭配-f选项
|
|||
|
[root@localhost 桌面]# cat s.sed
|
|||
|
s/bin/a/g
|
|||
|
s/ftp/b/g
|
|||
|
s/mail/c/g
|
|||
|
[root@localhost 桌面]# sed -f s.sed passwd | less
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
基本正则 sed
|
|||
|
|
|||
|
扩展正则 sed -r 无论是扩展正则还是基本正则,全部加r参数
|
|||
|
|
|||
|
command部分
|
|||
|
|
|||
|
```shell
|
|||
|
p 打印行 1p 输出再打印一遍第一行 1~2 打印奇数 0~2打印偶数
|
|||
|
d 删除文本
|
|||
|
#sed '1 d' passwd
|
|||
|
#sed '$ d' passwd
|
|||
|
#sed '1,3 d' passwd
|
|||
|
#sed '1,/^dian/ d' passwd
|
|||
|
a 追加文本(后)
|
|||
|
#sed '2 a nihao' passwd
|
|||
|
#sed '/^dian/ a nihao' passwd
|
|||
|
i 前插
|
|||
|
# sed -i '1 i nihao' passwd
|
|||
|
c 替换 sed '/zhong/c abc' 将zhong这一行替换成abc
|
|||
|
#sed -i '1 c no' passwd
|
|||
|
```
|
|||
|
|
|||
|
#### 3.sed案例
|
|||
|
|
|||
|
```shell
|
|||
|
1. sed可以从stdin中读取内容
|
|||
|
$ cat filename | sed 's/pattern/replace_string/'
|
|||
|
|
|||
|
2. 选项-i会使得sed用修改后的数据替换原文件
|
|||
|
$ sed -i 's/pattern/replace_string/' filename
|
|||
|
|
|||
|
3. g标记可以使sed执行全局替换
|
|||
|
$ sed 's/pattern/replace_string/g' filename
|
|||
|
|
|||
|
4. g标记可以使sed匹配第N次以后的字符被替换
|
|||
|
$ echo "thisthisthisthis" | sed 's/this/THIS/2g'
|
|||
|
|
|||
|
5. sed中的分隔符可以替换成别的字符, 因为s标识会认为后面的字符为分隔符
|
|||
|
$ sed 's:text:replace_text:'
|
|||
|
$ sed 's|text|replace_text|'
|
|||
|
|
|||
|
6. sed可以利用指令来删除文件中的空行
|
|||
|
$ sed '/^$/d' filename
|
|||
|
|
|||
|
7. 替换指定的字符串或数字
|
|||
|
$ cat sed_data.txt
|
|||
|
11 abc 111 this 9 file contains 111 11 99 numbers 0000
|
|||
|
$ sed -i 's/\b[0-9]\{3\}\b/NUMBER/g' sed_data.txt
|
|||
|
$ cat sed_data.txt
|
|||
|
11 abc NUMBER this 9 file contains NUMBER 11 99 numbers 0000
|
|||
|
|
|||
|
8. 由于在使用-i参数时比较危险, 所以我们在使用i参数时在后面加上.bak就会产生一个备份的文件,以防后悔
|
|||
|
$ sed -i.bak 's/pattern/replace_string/' filename
|
|||
|
```
|
|||
|
|
|||
|
## 二:文本处理awk
|
|||
|
|
|||
|
#### 1.awk介绍
|
|||
|
|
|||
|
awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk的处理文本和数据的方式是这样的,它逐行扫描文件,从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作。如果没有指定处理动作,则把匹配的行显示到标准输出(屏幕),如果没有指定模式,则所有被操作所指定的行都被处理。awk分别代表其作者姓氏的第一个字母。因为它的作者是三个人,分别是Alfred Aho、Brian Kernighan、Peter Weinberger。gawk是awk的GNU版本,它提供了Bell实验室和GNU的一些扩展
|
|||
|
|
|||
|
#### 2.语法格式
|
|||
|
|
|||
|
```shell
|
|||
|
awk [options] 'commands' filenames
|
|||
|
```
|
|||
|
|
|||
|
options部分
|
|||
|
|
|||
|
```shell
|
|||
|
POSIX options: GNU long options: (standard)
|
|||
|
-f progfile --file=progfile 指定awk脚本文件
|
|||
|
-F fs --field-separator=fs 定义输入字段分隔符,默认的分隔符是空格或制表符(tab)
|
|||
|
-v var=val --assign=var=val 定义变量并赋值
|
|||
|
```
|
|||
|
|
|||
|
command部分
|
|||
|
|
|||
|
```shell
|
|||
|
awk BEGIN{} {} END{} 文件
|
|||
|
BEGIN{} {} END{}
|
|||
|
行处理前 行处理 行处理后
|
|||
|
```
|
|||
|
|
|||
|
BEGIN{} 所有文本内容读入之前要执行的命令 可以不需要后面跟文件,因为他是在读入文件之前的操作
|
|||
|
|
|||
|
{} 主输入循环 读入一行命令执行一次循环
|
|||
|
|
|||
|
END{} 所有文本都读入完成之后执行的命令 必须要读入文件,因为他是在读入文件之后的操作
|
|||
|
|
|||
|
案例:
|
|||
|
|
|||
|
```shell
|
|||
|
# awk 'BEGIN{print 1/2} {print "ok"} END{print "-----------"}' /etc/hosts
|
|||
|
0.5
|
|||
|
ok
|
|||
|
ok
|
|||
|
ok
|
|||
|
-----------
|
|||
|
|
|||
|
BEGIN{} 通常用于定义一些变量,例如BEGIN{FS=":";OFS="---"}
|
|||
|
```
|
|||
|
|
|||
|
常用案例:
|
|||
|
|
|||
|
```shell
|
|||
|
awk 'pattern' filename 示例:awk -F: '/root/' /etc/passwd
|
|||
|
awk '{action}' filename 示例:awk -F: '{print $1}' /etc/passwd
|
|||
|
awk 'pattern {action}' filename 示例:awk -F: '/root/{print $1,$3}' /etc/passwd
|
|||
|
示例:awk 'BEGIN{FS=":"} /root/{print $1,$3}' /etc/passwd
|
|||
|
command |awk 'pattern {action}' 示例:df -P| grep '/' |awk '$4 > 25000 {print $4}'
|
|||
|
```
|
|||
|
|
|||
|
#### 3.工作原理
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print $1,$3}' /etc/passwd
|
|||
|
(1)awk使用一行作为输入,并将这一行赋给内部变量$0,每一行也可称为一个记录,以换行符结束
|
|||
|
|
|||
|
root : x : 0 : 0 : root : /root : /bin/bash
|
|||
|
1 2 3 4 5 6 7
|
|||
|
(2)然后,行被:(默认为空格或制表符)分解成字段(或域),每个字段存储在已编号的变量中,从$1开始,最多达100个字段
|
|||
|
|
|||
|
(3)awk如何知道用空格来分隔字段的呢? 因为有一个内部变量FS来确定字段分隔符。初始时,FS赋为空格
|
|||
|
|
|||
|
(4)awk打印字段时,将以设置的方法使用print函数打印,awk在打印的字段间加上空格,因为$1,$3之间有一个逗号。逗号比较特殊,它映射为另一个内部变量,称为输出字段分隔符OFS,OFS默认为空格
|
|||
|
|
|||
|
(5)awk输出之后,将从文件中获取另一行,并将其存储在$0中,覆盖原来的内容,然后将新的字符串分隔成字段并进行处理。该过程将持续到所有行处理完毕
|
|||
|
```
|
|||
|
|
|||
|
#### 4.内建变量
|
|||
|
|
|||
|
```shell
|
|||
|
$0: awk变量$0保存当前记录的内容
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print $0}' /etc/passwd
|
|||
|
NR: The total number of input records seen so far.
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print NR, $0}' /etc/passwd /etc/hosts
|
|||
|
FNR: The input record number in the current input file
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print FNR, $0}' /etc/passwd /etc/hosts
|
|||
|
NF: 保存记录的字段数,$1,$2...$100
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print $0,NF}' /etc/passwd
|
|||
|
FS: 输入字段分隔符,默认空格
|
|||
|
[root@xingdiancloud ~]# awk -F: '/alice/{print $1, $3}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F'[ :\t]' '{print $1,$2,$3}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"} {print $1,$3}' /etc/passwd
|
|||
|
OFS: 输出字段分隔符
|
|||
|
[root@xingdiancloud ~]# awk -F: '/alice/{print $1,$2,$3,$4}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"; OFS="+++"} /^root/{print $1,$2,$3,$4}' passwd
|
|||
|
RS The input record separator, by default a newline. 默认是回车
|
|||
|
[root@xingdiancloud ~]# awk -F: 'BEGIN{RS=" "} {print $0}' a.txt
|
|||
|
ORS The output record separator, by default a newline.
|
|||
|
[root@xingdiancloud ~]# awk -F: 'BEGIN{ORS=""} {print $0}' passwd
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
字段分隔符: FS OFS 默认空格或制表符
|
|||
|
|
|||
|
记录分隔符: RS ORS 默认换行符
|
|||
|
|
|||
|
案例:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{ORS=" "} {print $0}' /etc/passwd
|
|||
|
#将文件每一行合并为一行
|
|||
|
ORS默认输出一条记录应该回车,加了一个空格
|
|||
|
|
|||
|
[root@xingdiancloud ~]# head -1 /etc/passwd > passwd1
|
|||
|
[root@xingdiancloud ~]# cat passwd1
|
|||
|
root:x:0:0:root:/root:/bin/bash
|
|||
|
[root@xingdiancloud ~]#
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{RS=":"} {print $0}' passwd1
|
|||
|
root
|
|||
|
x
|
|||
|
0
|
|||
|
0
|
|||
|
root
|
|||
|
/root
|
|||
|
/bin/bash
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{RS=":"} {print $0}' passwd1 |grep -v '^$' > passwd2
|
|||
|
```
|
|||
|
|
|||
|
#### 5.格式化输出
|
|||
|
|
|||
|
print函数
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# date |awk '{print "Month: " $2 "\nYear: " $NF}'
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print "username is: " $1 "\t uid is: " $3}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print "\tusername and uid: " $1,$3 "!"}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
printf函数
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{printf "|%-15s| %-10s| %-15s|\n", $1,$2,$3}' /etc/passwd
|
|||
|
|
|||
|
%s 字符类型
|
|||
|
%d 数值类型
|
|||
|
%f 浮点类型
|
|||
|
占15字符
|
|||
|
- 表示左对齐,默认是右对齐
|
|||
|
printf默认不会在行尾自动换行,加\n
|
|||
|
```
|
|||
|
|
|||
|
#### 6.awk模式和动作
|
|||
|
|
|||
|
任何awk语句都由模式和动作组成。模式部分决定动作语句何时触发及触发事件。处理即对数据进行的操作。如果省略模式部分,动作将时刻保持执行状态。模式可以是任何条件语句或复合语句或正则表达式。模式包括两个特殊字段 BEGIN和END。使用BEGIN语句设置计数和打印头。BEGIN语句使用在任何文本浏览动作之前,之后文本浏览动作依据输入文本开始执行。END语句用来在awk完成文本浏览动作后打印输出文本总数和结尾状态
|
|||
|
|
|||
|
##### 模式
|
|||
|
|
|||
|
正则表达式
|
|||
|
|
|||
|
```shell
|
|||
|
匹配记录(整行):~匹配
|
|||
|
[root@xingdiancloud ~]# awk '/^alice/' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk '$0 ~ /^alice/' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk '!/alice/' passwd
|
|||
|
[root@xingdiancloud ~]# awk '$0 !~ /^alice/' /etc/passwd
|
|||
|
|
|||
|
匹配字段:匹配操作符(~ !~)
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1 ~ /^alice/' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$NF !~ /bash$/' /etc/passw
|
|||
|
```
|
|||
|
|
|||
|
比较表达式
|
|||
|
|
|||
|
比较表达式采用对文本进行比较,只有当条件为真,才执行指定的动作。比较表达式使用关系运算符,用于比较数字与字符串
|
|||
|
|
|||
|
```shell
|
|||
|
运算符 含义 示例
|
|||
|
< 小于 x<y
|
|||
|
<= 小于或等于 x<=y
|
|||
|
== 等于 x==y
|
|||
|
!= 不等于 x!=y
|
|||
|
>= 大于等于 x>=y
|
|||
|
> 大于 x>y
|
|||
|
```
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '$3 == 0' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$3 < 10' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$NF == "/bin/bash"' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1 == "alice"' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1 ~ /alic/ ' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1 !~ /alic/ ' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# df -P | grep '/' |awk '$4 > 25000'
|
|||
|
```
|
|||
|
|
|||
|
条件表达式
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '$3>300 {print $0}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{ if($3>300) {print $0} }' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{ if($3>300) {print $3} else{print $1} }' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
算术运算
|
|||
|
|
|||
|
```shell
|
|||
|
+ - * / %(模) ^(幂2^3)
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk -F: '$3 * 10 > 500' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{ if($3*10>500){print $0} }' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
逻辑操作符和复合模式
|
|||
|
|
|||
|
```shell
|
|||
|
&& 逻辑与 a&&b
|
|||
|
|| 逻辑或 a||b
|
|||
|
! 逻辑非 !a 除了这个以外的
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1~/root/ && $3<=15' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '$1~/root/ || $3<=15' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '!($1~/root/ || $3<=15)' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
##### 示例
|
|||
|
|
|||
|
```ini
|
|||
|
[root@xingdiancloud ~]# awk '/west/' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/^north/' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$3 ~ /^north/' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/^(no|so)/' datafile
|
|||
|
[root@xingdiancloud ~]# awk '{print $3,$2}' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '{print $3 $2}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '{print $0}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/northeast/{print $3,$2}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/E/' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '/^[ns]/{print $1}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$5 ~ /\.[7-9]+/' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$2 !~ /E/{print $1,$2}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$3 ~ /^Joel/{print $3 " is a nice boy."}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$8 ~ /[0-9][0-9]$/{print $8}' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '$4 ~ /Chin$/{print "The price is $" $8 "."}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/Tj/{print $0}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '{print $1}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print $1}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk '{print "Number of fields: "NF}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print "Number of fields: "NF}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F"[ :]" '{print NF}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F"[ :]+" '{print NF}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk '$7 == 5' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$2 == "CT" {print $1, $2}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$7 != 5' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# cat b.txt
|
|||
|
xingdian sheng:is a::good boy!
|
|||
|
[root@xingdiancloud ~]# awk '{print NF}' b.txt
|
|||
|
4
|
|||
|
[root@xingdiancloud ~]# awk -F: '{print NF}' b.txt
|
|||
|
4
|
|||
|
[root@xingdiancloud ~]# awk -F"[ :]" '{print NF}' b.txt
|
|||
|
7
|
|||
|
[root@xingdiancloud ~]# awk -F"[ :]+" '{print NF}' b.txt
|
|||
|
6
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '$7 < 5 {print $4, $7}' datafile #{if($7<5){print $4,$7}}
|
|||
|
[root@xingdiancloud ~]# awk '$6 > 9 {print $1,$6}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$8 <= 17 {print $8}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$8 >= 17 {print $8}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '$8 > 10 && $8 < 17' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '$2 == "NW" || $1 ~ /south/ {print $1, $2}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '!($8 == 13){print $8}' datafile #$8 != 13
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $5 + 10}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $8 + 10}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $5 + 10.56}' datafile
|
|||
|
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $8 - 10}' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $8 / 2 }' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $8 / 3 }' datafile
|
|||
|
[root@xingdiancloud ~]# awk '/southem/{print $8 * 2 }' datafile
|
|||
|
```
|
|||
|
|
|||
|
#### 7.脚本编程-条件判断
|
|||
|
|
|||
|
if语句
|
|||
|
|
|||
|
```shell
|
|||
|
{if(表达式){语句;语句;...}}
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0) {print $1 " is administrator."}}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3>0 && $3<1000){count++;}} END{print count}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
if...else语句
|
|||
|
|
|||
|
```shell
|
|||
|
{if(表达式){语句;语句;...}else{语句;语句;...}}
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0){print $1} else {print $7}}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0) {count++} else{i++} }' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0){count++} else{i++}} END{print "管理员个数: "count ; print "系统用户数: "i}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
if...else if...else语句
|
|||
|
|
|||
|
```shell
|
|||
|
{if(表达式1){语句;语句;...}else if(表达式2){语句;语句;...}else if(表达式3){语句;语句;...}else{语句;语句;...}
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0){i++} else if($3>999){k++} else{j++}} END{print i; print k; print j}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{if($3==0){i++} else if($3>999){k++} else{j++}} END{print "管理员个数: "i; print "普通用个数: "k; print "系统用户: "j}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
#### 8.脚本编程-循环
|
|||
|
|
|||
|
while:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{ i=1; while(i<=10){print i; i++} }'
|
|||
|
[root@xingdiancloud ~]# awk -F: '/^root/{i=1; while(i<=7){print $i; i++}}' passwd
|
|||
|
[root@xingdiancloud ~]# awk '{i=1; while(i<=NF){print $i; i++}}' /etc/hosts
|
|||
|
[root@xingdiancloud ~]# awk -F: '{i=1; while(i<=10) {print $0; i++}}' /etc/passwd //将每行打印10次
|
|||
|
[root@xingdiancloud ~]# cat b.txt
|
|||
|
111 222
|
|||
|
333 444 555
|
|||
|
666 777 888 999
|
|||
|
[root@xingdiancloud ~]# awk '{i=1; while(i<=NF){print $i; i++}}' b.txt //分别打印每行的每列
|
|||
|
111
|
|||
|
222
|
|||
|
333
|
|||
|
444
|
|||
|
555
|
|||
|
666
|
|||
|
777
|
|||
|
888
|
|||
|
999
|
|||
|
```
|
|||
|
|
|||
|
for:
|
|||
|
|
|||
|
```she
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{for(i=1;i<=5;i++){print i} }' //C风格for
|
|||
|
1
|
|||
|
2
|
|||
|
3
|
|||
|
4
|
|||
|
5
|
|||
|
[root@xingdiancloud ~]# awk -F: '{ for(i=1;i<=10;i++) {print $0} }' /etc/passwd //将每行打印10次
|
|||
|
[root@xingdiancloud ~]# awk -F: '{ for(i=1;i<=NF;i++) {print $i} }' passwd //分别打印每行的每列
|
|||
|
root
|
|||
|
x
|
|||
|
0
|
|||
|
0
|
|||
|
root
|
|||
|
/root
|
|||
|
/bin/bash
|
|||
|
bin
|
|||
|
x
|
|||
|
1
|
|||
|
1
|
|||
|
bin
|
|||
|
/bin
|
|||
|
/sbin/nologin
|
|||
|
```
|
|||
|
|
|||
|
#### 9.脚本编程-数组
|
|||
|
|
|||
|
案例一:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[i++]=$1} END{print username[1]}' /etc/passwd
|
|||
|
bin
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[i++]=$1} END{print username[0]}' /etc/passwd
|
|||
|
root
|
|||
|
```
|
|||
|
|
|||
|
数组遍历:
|
|||
|
|
|||
|
按元素个数遍历
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[x++]=$1} END{for(i=0;i<x;i++) print i,username[i]}' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[++x]=$1} END{for(i=1;i<=x;i++) print i,username[i]}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
按索引遍历
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[x++]=$1} END{for(i in username) {print i,username[i]} }' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# awk -F: '{username[++x]=$1} END{for(i in username) {print i,username[i]} }' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
综合案例:
|
|||
|
|
|||
|
统计/etc/passwd中各种类型shell的数量(统计谁把谁当作索引)
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"} {shells[$NF]++} END{for(i in shells){print i,shells[i]}}' /etc/passwd
|
|||
|
```
|
|||
|
|
|||
|
统计TCP不同状态的数量
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# netstat -ant |grep tcp |awk '{access_stat[$NF]++} END{for(i in access_stat ){print i,access_stat[i]}}'
|
|||
|
```
|
|||
|
|
|||
|
## 三:文本过滤grep
|
|||
|
|
|||
|
#### 1.grep介绍
|
|||
|
|
|||
|
grep: 在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行
|
|||
|
|
|||
|
egrep: 扩展的egrep,支持更多的正则表达式元字符
|
|||
|
|
|||
|
fgrep: 固定grep(fixed grep),有时也被称作快速(fast grep),它按字面解释所有的字符(了解)
|
|||
|
|
|||
|
#### 2.语法格式
|
|||
|
|
|||
|
```shell
|
|||
|
grep [选项] PATTERN filename filename ...
|
|||
|
```
|
|||
|
|
|||
|
案例:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# grep 'Tom' /etc/passwd
|
|||
|
[root@xingdiancloud ~]# grep 'bash shell' /etc/test
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
找到: grep返回的退出状态为0
|
|||
|
没找到: grep返回的退出状态为1
|
|||
|
找不到指定文件: grep返回的退出状态为2
|
|||
|
|
|||
|
来自标准输入或管道:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# ps aux |grep 'sshd'
|
|||
|
[root@xingdiancloud ~]# ll |grep '^d'
|
|||
|
[root@xingdiancloud ~]# grep 'alice' /etc/passwd /etc/shadow /etc/group
|
|||
|
```
|
|||
|
|
|||
|
#### 2.使用元字符
|
|||
|
|
|||
|
```shell
|
|||
|
grep: 使用基本元字符集 ^, $, ., *, [], [^], \< \>,\(\),\{\}, \+, \|
|
|||
|
egrep(或grep -E): 使用扩展元字符集 ?, +, { }, |, ( )
|
|||
|
注:grep也可以使用扩展集中的元字符,仅需要对这些元字符前置一个反斜线
|
|||
|
|
|||
|
\w 所有字母与数字,称为字符[a-zA-Z0-9] 'l[a-zA-Z0-9]*ve' 'l\w*ve'
|
|||
|
\W 所有字母与数字之外的字符,称为非字符 'love[^a-zA-Z0-9]+' 'love\W+'
|
|||
|
\b 词边界 '\<love\>' '\blove\b'
|
|||
|
```
|
|||
|
|
|||
|
#### 3.grep示例
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# egrep 'N\W' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '^n' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '4$' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '5\..' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '\.5' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '^[we]' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '[^0-9]' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '[A-Z][A-Z] [A-Z]' datafile
|
|||
|
[root@xingdiancloud ~]# egrep 'ss* ' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '[a-z]{9}' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '\<north' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '\<north\>' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '\<[a-r].*n\>' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '^n\w*\W' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '\bnorth\b' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '3+' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '2\.?[0-9]' datafile
|
|||
|
[root@xingdiancloud ~]# egrep '(no)+' datafile
|
|||
|
[root@xingdiancloud ~]# egrep 'S(h|u)' datafile
|
|||
|
```
|
|||
|
|
|||
|
#### 4.grep参数
|
|||
|
|
|||
|
```shell
|
|||
|
-i, --ignore-case 忽略大小写
|
|||
|
-l, --files-with-matches 只列出匹配行所在的文件名
|
|||
|
-n, --line-number 在每一行前面加上它在文件中的相对行号
|
|||
|
-c, --count 显示成功匹配的行数
|
|||
|
-s, --no-messages 禁止显示文件不存在或文件不可读的错误信息
|
|||
|
-q, --quiet, --silent 静默--quiet, --silent
|
|||
|
-v, --invert-match 反向查找,只显示不匹配的行
|
|||
|
-R, -r, --recursive 递归针对目录
|
|||
|
--color 颜色
|
|||
|
-o, --only-matching 只显示匹配的内容
|
|||
|
```
|
|||
|
|
|||
|
示例:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdian ~]# grep -R 'ifcfg' /etc 目录
|
|||
|
|
|||
|
[root@xingdian ~]# egrep 'root' /etc/passwd /etc/shadow /etc/hosts
|
|||
|
/etc/passwd:root:x:0:0:root:/root:/bin/bash
|
|||
|
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
|
|||
|
/etc/shadow:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUYfw7vJW.78.uRXCLIxw4mBk82Z99:7:::
|
|||
|
|
|||
|
[root@xingdian ~]# egrep -l 'root' /etc/passwd /etc/shadow /etc/hosts
|
|||
|
/etc/passwd
|
|||
|
/etc/shadow
|
|||
|
|
|||
|
[root@xingdian ~]# egrep -n 'root' /etc/passwd /etc/shadow /etc/hosts
|
|||
|
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
|
|||
|
/etc/passwd:11:operator:x:11:0:operator:/root:/sbin/nologin
|
|||
|
/etc/shadow:1:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUy8.M78.uRXCLIxw4mBk82ZrNlxyf54
|
|||
|
|
|||
|
|
|||
|
[root@xingdian ~]# egrep -R '54:04:A6:CE:C2:1F' /etc/sysconfig/
|
|||
|
|
|||
|
[root@xingdian ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
|
|||
|
192.168.2.254
|
|||
|
[root@xingdian ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
|
|||
|
192.168.2.254
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|