shell脚本——grep cut sort uniq tee diff paster tr等小工具的使用方法

grep

这篇文件对于grep只是简单介绍 因为grep的很多用法需要配合正则表达式 后面记录正则的文章会详细解释grep

grep是行过滤工具;用于根据关键字进行行过滤

语法和选项

语法:

grep [选项] ‘关键字’ 文件名

常见选项:
平时常用会高亮

-i : 不区分大小写
-v: 查找不包含指定内容的行,反向选择
-w : 按单词搜索
-o : 打印匹配关键字
-c : 统计匹配到的行数
-n : 显示行号
-r: 逐层遍历目录查找
-A : 显示匹配行及后面多少行
-B : 显示匹配行及前面多少行
-C : 显示匹配行前后多少行
-l :只列出匹配的文件名
-L :列出不匹配的文件名
-e : 使用正则匹配
-E :使用扩展正则匹配
^key :以关键字开头
key$ :以关键字结尾
^$:匹配空行
^#:匹配注释行(一般和-v一起使用 匹配非注释行)

实例

# -v 
# 查找非注释行
grep -v "^#" /etc/vsftpd/vsftpd.conf

# 如果注释前面有空格和#
grep -v "^ *#"

# 同时排除注释行和有空格的注释行
grep -Ev '^#|^ *#'

# -w
# 用于字符串精确匹配
grep -w 'root' /etc/passwd

ifconfig |grep -w 'inet'

# -o
# 只打印匹配的关键字
vmstat |grep -Eo '\<wa\>'

grep '^IPA' /etc/sysconfig/network-scripts/ifcfg-ens33 |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'

# -r -R
# 假如你想从某个文件中查找一段关键字 就可以使用-r和-R 逐层遍历目录查找 
# 比如我查找'vmstat|awk' 然后就能查找出在什么路径的什么文件中有写!

[root@maomao shell]# grep -R 'vmstat|awk' /root
/root/1.26/system.sh:vmstat|awk '{if(NR==3)print "等待在CPU资源的进程数:" $1}'
/root/1.26/system.sh:vmstat|awk '{if(NR==3)print "CPU剩余:" $15 "%"}'
[root@maomao shell]# grep -r 'vmstat|awk' /root
/root/1.26/system.sh:vmstat|awk '{if(NR==3)print "等待在CPU资源的进程数:" $1}'
/root/1.26/system.sh:vmstat|awk '{if(NR==3)print "CPU剩余:" $15 "%"}'

# ^和$
[root@maomao shell]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

[root@maomao shell]# grep 'nologin$' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

# ^$ 匹配空行 比如你不想查看一篇文章中的空行
grep -v '^$' 

# -n 显示行号 一般和vim +行号一起使用 直接跳转到25行
[root@maomao shell]# grep -n 'maomao' passwd 
25:maomao:x:1002:1002::/home/maomao:/bin/bash
[root@maomao shell]# vim passwd +25

cut

cut是列截取工具,用于列的截取

语法和选项

语法:

cut 选项 文件名

扫描二维码关注公众号,回复: 12922857 查看本文章

常见选项:
-c: 以字符为单位进行分割,截取
-d: 自定义分隔符,默认为制表符\t
-f: 与-d一起使用,指定截取哪个区域

实例

# -c	
[root@maomao shell]# cut -c1-5 passwd 
root:
[root@maomao shell]# cut -c1,5 passwd 
r:
# cut -c10- 表示以字符分割第十个字符到最后一个
[root@maomao shell]# cut -c10- passwd 
0:root:/root:/bin/bash
:bin:/bin:/sbin/nologin

# -d -f	 -d后面加自定义分隔符 -f代表截取的区域
[root@maomao shell]# cut -d: -f7 /etc/passwd |sort |uniq -c
     10 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     25 /sbin/nologin
      1 /sbin/shutdown

# cut其实就有awk的截取功能 但是和awk比还是简单了一些
[root@maomao shell]# awk -F: '{print $NF}' /etc/passwd |sort |uniq -c
     10 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     25 /sbin/nologin
      1 /sbin/shutdown

# cut -d: -f1 1.txt             以:冒号分割,截取第1列内容
# cut -d: -f1,6,7 1.txt     以:冒号分割,截取第1,6,7列内容
# cut -c4 1.txt                 截取文件中每行第4个字符
# cut -c1-4 1.txt             截取文件中每行的1-4个字符
# cut -c4-10 1.txt             截取文件中每行的4-10个字符
# cut -c5- 1.txt                 从第5个字符开始截取后面所有字符

练习

# 用grep和cut列出你当系统的运行级别
runlevel |cut -c3
runlevel | cut -d ' ' -f2
grep -v '^#' /etc/inittab | cut -d: -f2
grep '^id' /etc/inittab |cut -d: -f2
grep "initdefault:$" /etc/inittab | cut -c4
grep -v ^# /etc/inittab |cut -c4
grep 'id:' /etc/inittab |cut -d: -f2
cut -d':' -f2 /etc/inittab |grep -v ^#
cut -c4 /etc/inittab |tail -1
cut -d: -f2 /etc/inittab |tail -1

sort

sort工具用于排序;它将文件的每一行作为一个单位,从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。

语法和选项

-u :去除重复行
-r :降序排列,默认是升序
-o : 将排序结果输出到文件中,类似重定向符号>
-n :以数字排序,默认是按字符排序
-t :分隔符
-k :第N列
-b :忽略前导空格。
-R :随机排序,每次运行的结果均不同

-t -k 就是和cut的-d -f类似

实例

sort -n -t: -k3 passwd     # 按照用户的uid进行升序排列
sort -nr -t: -k3 passwd    # 按照用户的uid进行降序排列
sort -n            # 按照数字排序
sort -nu           # 按照数字排序并且去重
sort -n number.txt -o new.txt     # 按照数字排序并将结果重定向到文件
sort -R 		   # 随机排序
sort -u            # 去重

# 统计端口号状态数量
[root@maomao shell]# ss -antpl |grep -v 'State' |cut -d' ' -f1 |sort |uniq -c
      7 LISTEN


uniq

uniq用于去除连续的重复行

语法和选项

-i: 忽略大小写
-c: 统计重复行次数
-d:只显示重复行

实例

# 自己写一个number.txt去试试
uniq number.txt
uniq -d number.txt
uniq -dc number.txt

# 常用的地方一般就是统计数量 配合awk和sort
[root@maomao shell]# ss -antpl |grep -v 'State' |awk -F' ' '{print $1}'|sort |uniq -c
      7 LISTEN

tee

tee工具是从标准输入读取并写入到标准输出和文件,即:双向覆盖重定向(屏幕输出|文本输入)

语法和选项

-a 双向追加重定向

tee和 >、>>的区别

[root@maomao shell]# echo "hello" > mao
[root@maomao shell]# cat mao 
hello
[root@maomao shell]# echo "world" > mao
[root@maomao shell]# cat mao 
world
[root@maomao shell]# echo "hello" >> mao
[root@maomao shell]# cat mao 
world
hello
[root@maomao shell]# echo "maomao" |tee mao 
maomao
[root@maomao shell]# cat mao 
maomao
[root@maomao shell]# echo "nihaoya" |tee -a mao 
nihaoya
[root@maomao shell]# cat mao 
maomao
nihaoya

总结: 通过|tee 可以实现覆盖重定向>的功能
|tee -a 可实现追加重定向>>的功能
tee同时会将输出结果打印到屏幕上

实例

[root@maomao shell]# tee mao.txt <<-EOF
> hello
> world
> 123
> EOF
hello
world
123

# 配置docker镜像加速器会使用到tee和<<-EOF
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    
    
  "registry-mirrors": ["https://mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

练习

# 将/etc/vsftpd/vsftpd.conf 备份 并且只要没注释的部分

grep -v '^#' /etc/vsftpd/vsftpd.conf |grep -v '^$' |tee vsftpd.conf.bak

diff

diff工具用于逐行比较文件的不同

注意:diff描述两个文件不同的方式是告诉我们怎样改变第一个文件之后与第二个文件匹配

语法和选项

语法

diff [选项] 文件1 文件2

常用选项
-b 不检查空格
-B 不检查空白行
-i 不检查大小写
-w 忽略所有的空格
–normal 正常格式显示(默认)
-c 上下文格式显示
-u 合并格式显示

实例

# 比较两个普通文件异同,文件准备
[root@maomao ~]# cat file1
aaaa
111
hello world
222
333
bbb
[root@maomao ~]#
[root@maomao ~]# cat file2
aaa
hello
111
222
bbb
333
world

1)正常显示

diff目的:file1如何改变才能和file2匹配
[root@maomao ~]# diff file1 file2
1c1,2                    第一个文件的第1行需要改变(c=change)才能和第二个文件的第1到2行匹配            
< aaaa                小于号"<"表示左边文件(file1)文件内容
---                    ---表示分隔符
> aaa                    大于号">"表示右边文件(file2)文件内容
> hello
3d3                    第一个文件的第3行删除(d=delete)后才能和第二个文件的第3行匹配
< hello world
5d4                    第一个文件的第5行删除后才能和第二个文件的第4行匹配
< 333
6a6,7                    第一个文件的第6行增加(a=add)内容后才能和第二个文件的第6到7行匹配
> 333                    需要增加的内容在第二个文件里是333和world
> world

2)上下文格式显示

[root@maomao ~]# diff -c file1 file2
前两行主要列出需要比较的文件名和文件的时间戳;文件名前面的符号***表示file1,---表示file2
*** file1       2021-01-26 18:26:05.748650262 +0800
--- file2       2021-01-26 18:26:30.470646030 +0800
***************    我是分隔符
*** 1,6 *******开头表示file1文件,1,6表示1到6行
! aaaa                !表示该行需要修改才与第二个文件匹配
  111
- hello world        -表示需要删除该行才与第二个文件匹配
  222
- 333                    -表示需要删除该行才与第二个文件匹配
  bbb
--- 1,7 -------开头表示file2文件,1,7表示1到7行
! aaa                    表示第一个文件需要修改才与第二个文件匹配
! hello                表示第一个文件需要修改才与第二个文件匹配
  111
  222
  bbb
+ 333                    表示第一个文件需要加上该行才与第二个文件匹配
+ world                表示第一个文件需要加上该行才与第二个文件匹配

3)合并格式显示

[root@maomao ~]# diff -u file1 file2
前两行主要列出需要比较的文件名和文件的时间戳;文件名前面的符号---表示file1,+++表示file2
--- file1       2021-01-26 18:26:05.748650262 +0800
+++ file2       2021-01-26 18:26:30.470646030 +0800
@@ -1,6 +1,7 @@
-aaaa
+aaa
+hello
 111
-hello world
 222
-333
 bbb
+333
+world
  • 比较两个目录不同
默认情况下也会比较两个目录里相同文件的内容
[root@maomao  tmp]# diff dir1 dir2
diff dir1/file1 dir2/file1
0a1
> hello
Only in dir1: file3
Only in dir2: test1
如果只需要比较两个目录里文件的不同,不需要进一步比较文件内容,需要加-q选项
[root@maomao  tmp]# diff -q dir1 dir2
Files dir1/file1 and dir2/file1 differ
Only in dir1: file3
Only in dir2: test1

其他小技巧:

有时候我们需要以一个文件为标准,去修改其他文件,并且修改的地方较多时,我们可以通过打补丁的方式完成。

1)先找出文件不同,然后输出到一个文件
[root@maomao ~]# diff -uN file1 file2 > file.patch
-u:上下文模式
-N:将不存在的文件当作空文件
2)将不同内容打补丁到文件
[root@maomao ~]# patch file1 file.patch
patching file file1
3)测试验证
[root@maomao ~]# diff file1 file2
[root@maomao ~]#

paster

paste工具用于合并文件行

语法和选项

-d:自定义间隔符,默认是tab
-s:串行处理,非并行

实例

# 准备两个文件
[root@maomao shell]# cat file1 
hello world
888
[root@maomao shell]# cat file2 
nihao
999
ooooo
# 默认制表符分隔
[root@maomao shell]# paste file1 file2
hello world	nihao
888	999
	ooooo
# -d 自定义分隔符
[root@maomao shell]# paste -d% file1 file2
hello world%nihao
888%999
%ooooo

# -s 串行处理
[root@maomao shell]# paste -s file1 file2
hello world	888
nihao	999	ooooo

tr

tr用于字符转换,替换和删除;主要用于删除文件中控制字符或进行字符转换

语法和选项

语法:
用法1:

  • 命令的执行结果交给tr处理,其中string1用于查询,string2用于转换处理

  • commands|tr ‘string1’ ‘string2’

用法2:

  • tr处理的内容来自文件,记住要使用"<"标准输入

  • tr ‘string1’ ‘string2’ < filename

用法3:

  • 匹配string1进行相应操作,如删除操作

  • tr [options] ‘string1’ < filename

常用选项:
-d 删除字符串1中所有输入字符。
-s 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串

常用匹配字符串:

字符串 含义 备注
a-z或[:lower:] 匹配所有小写字母 所有大小写和数字[a-zA-Z0-9]
A-Z或[:upper:] 匹配所有大写字母
0-9或[:digit:] 匹配所有数字
[:alnum:] 匹配所有字母和数字
[:alpha:] 匹配所有字母
[:blank:] 所有水平空白
[:punct:] 匹配所有标点符号
[:space:] 所有水平或垂直的空格

实例

# 把一个文件中的小写字母全换成大写字母
tr 'a-z' 'A-Z' < passwd 

# 将数字替换成@符号
tr ‘0-9’ ‘@’ < 1.txt

# -d 
# 删除#
tr -d '#' < 1.txt

# -s删除重复的字符串
tr -s 'a-z' <passwd

练习

# 使用小工具分别截取当前主机IP;截取NETMASK;截取广播地址

# ifconfig eth0|grep 'Bcast'|tr -d '[a-zA-Z ]'|cut -d: -f2,3,4|tr ':' '\n'
10.1.1.1
10.1.1.255
255.255.255.0

# 将系统中所有普通用户的用户名、密码和默认shell保存到一个文件中,要求用户名密码和默认shell之间用tab键分割
grep 'bash$' passwd |grep -v 'root'|cut -d: -f1,2,7|tr ':' '\t' |tee passwd.txt

猜你喜欢

转载自blog.csdn.net/Cantevenl/article/details/115255574