shell 脚本中常用命令(diff、cut、patch、sort、uniq、&&和||、test)

1.diff 命令

(1)diff命令的用法:

  • diff 命令是用来比较两个文件或目录的不同
    diff [options] target1 target2
    diff file1 file2
    diff direcory1 directory2

(2)diff 在比较文件过程中结果读取方式

[num1 , num2][a|c|d][num3,num4]
num1,num2 表示在第一个文件中的行数
< 表示第一个文件中的内容, > 表示第二个文件中的内容, 
num3,num4 表示在第二个文件中的行数
2,4c2,4 表示改变第一个文件中的第二行和第四行才能匹配第二个文件中的第二行和第四行
a 表示添加------- add
c 表示更改-------change
d 表示删除 ----delete

例:
比较两个文件的不同
<1>切换到mnt下,将目录清理干净

[root@shell_example ~]# cd /mnt
[root@shell_example mnt]# rm -fr *
[root@shell_example mnt]# ls

在这里插入图片描述
<2>编辑两个文件test 和 test1

[root@shell_example mnt]# vim test
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# ls
test  test1
[root@shell_example mnt]# cat test
hello westos
[root@shell_example mnt]# cat test1
hello westos
123

在这里插入图片描述
<3>diff 比较两个文件的不同

##显示第一个文件中添加第二个文件第二行的123才可以匹配,
[root@shell_example mnt]# diff test test1
1a2
> 123

在这里插入图片描述

##改变顺序后显示第一个文件中的第二行删除后才可以和第二个文件的第一行匹配
[root@shell_example mnt]# diff test1 test
2d1
< 123

在这里插入图片描述
<4>编辑test文件,在文将中添加一个空白行

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos

在这里插入图片描述
在这里插入图片描述
<5>再次比较两个文件中的内容

[root@shell_example mnt]# diff test1 test
2c2
< 123
---
> 
##显示第一个文件改变第二行后才能和第二个文件的第二行匹配,
第一个文件中的内容是123,第二个文件中的内容是空白

在这里插入图片描述
改变文件顺序后再次比较

[root@shell_example mnt]# diff test test1
2c2
< 
---
> 123
##显示第一个文件改变第二行才可以匹配,第一个文件中的内容是空白行,
第二个文件中的内容是123

在这里插入图片描述
(3)diff 在比较目录过程中结果的读取

  • Only in directroy/: filename

directory 表示在那个目录中
filename 表示在这个目录

[root@shell_example mnt]# mkdir westos
[root@shell_example mnt]# mkdir westos1
[root@shell_example mnt]# ls
test  test1  test1.c  test.c  test.path  westos  westos1
[root@shell_example mnt]# diff westos westos1
[root@shell_example mnt]# touch westos/file1
[root@shell_example mnt]# diff westos westos1
Only in westos: file1
[root@shell_example mnt]# diff -r westos westos1
Only in westos: file1

在这里插入图片描述
在这里插入图片描述
4.diff 中常用的参数

-b 或 --ignore-space-change 不检查空格字符的不同
-B 或 --ignore-blank-lines 不检查空白行
-c 显示全部内文,并标出不同之处
-i 或 --ignore-case 不检查大小写的不同
-p :若比较的文件为 C 语言的程序码文件时,显示差异所在的函数名称;
-q 或 --brief 仅显示有无差异,不显示详细的信息
-r 或 --recursive 比较子目录中的文件
-u 以合并的方式来显示文件内容的不同

例:
<1>-b

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello  westos
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
[root@shell_example mnt]# diff test test1
1c1
< hello  westos
---
> hello westos
[root@shell_example mnt]# diff -b  test test1
test中有两个空格,而test1中只有一个空格,
不加-b时比较会提示第一个文件改变第一行才可以和第二个文件匹配,
但是加了-b之后忽略空格字符的不同,不会报错

在这里插入图片描述
<2>-B

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
linux
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
linux

[root@shell_example mnt]# diff test test1
2a3
> 
[root@shell_example mnt]# diff -B test test1

可以看到test中只有两行,但test1中有三行,前两行内容和test中相同,第三行是空白行,不加-B比较时提示,第一个文件中添加第二和文件的第三行空白行才可以匹配,加上-B之后不会报错,不检查空白行
在这里插入图片描述
<3>-c(1/2/3)

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
linux
123

haha
python
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
linux
123
redhat 
haha
python

在这里插入图片描述
1>-c 将所有的内容都显示出来,标记不同之处

[root@shell_example mnt]# diff -c test test1
*** test	2019-03-27 05:17:33.136526019 -0400
--- test1	2019-03-27 05:17:47.086526019 -0400
***************
*** 1,6 ****
  hello westos
  linux
  123
! 
  haha
  python
--- 1,6 ----
  hello westos
  linux
  123
! redhat 
  haha
  python

在这里插入图片描述
<2>-c1 显示不同前一条和不同后一条

[root@shell_example mnt]# diff -c1 test test1
*** test	2019-03-27 05:17:33.136526019 -0400
--- test1	2019-03-27 05:17:47.086526019 -0400
***************
*** 3,5 ****
  123
! 
  haha
--- 3,5 ----
  123
! redhat 
  haha

在这里插入图片描述
<3>-c2 显示不同的前两行和后两行

[root@shell_example mnt]# diff -c2 test test1
*** test	2019-03-27 05:17:33.136526019 -0400
--- test1	2019-03-27 05:17:47.086526019 -0400
***************
*** 2,6 ****
  linux
  123
! 
  haha
  python
--- 2,6 ----
  linux
  123
! r
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值