目录
一、diff
1、用法
2、输出信息
输出信息 | [num1 , num2] [a|c|d] [num3,num4] |
num1 , num2 | 第一个文件中的行 |
a | 添加 |
c | 更改 |
d | 删除 |
< | 第一个文件中的内容 |
> | 第二个文件中的内容 |
num3 , num4 | 第二个文件中的行 |
3、常用参数
-b | 忽略空格 | -c | 显示文件所有内容并标示不同 |
-B | 忽略空行 | -r | 对比目录 |
-i | 忽略大小写 | -u | 合并输出 |
二、patch
用法:
patch 原文件 补丁文件
-b 打补丁时备份原文件
先安装patch软件
测试:先创建test文件和test.new文件,文件差别如图所示。以test为标准,test.new为更新后的文件,生成补丁文件test.patch。通过<patch>命令将补丁打入test文件中,那么test文件和test.new文件会一模一样。
[root@shell_1 test]# cat test
123
hahaha
456
[root@shell_1 test]# cat test.new
123
hahaha hello
456
111
[root@shell_1 test]# diff -u test test.new > test.patch
[root@shell_1 test]# cat test.patch
--- test 2022-05-17 11:04:13.464921389 +0800
+++ test.new 2022-05-17 11:04:53.813132944 +0800
@@ -1,3 +1,4 @@
123
-hahaha
+hahaha hello
456
+111
[root@shell_1 test]#
[root@shell_1 test]# patch -b test test.patch
patching file test
[root@shell_1 test]# diff test test.new
生成补丁文件是以第二个文件为标准的。
三、cut
-d : | 指定“:”为分隔符 |
-f 5 | 指定显示第五列 5 #显示第五列 3,5 #显示第三列和第五列 3-5 #显示第三列到第五列 5- #显示第五列以后 -5 #显示第五列以前 |
-c | 指定截取的字符(数字用法同-f) |
命令测试
[root@shell_1 test]# head -n5 /etc/passwd > ./passwd
[root@shell_1 test]# cut -d : -f 6 passwd
[root@shell_1 test]# cut -d : -f -6 passwd
[root@shell_1 test]# cut -c 1 passwd
[root@shell_1 test]# cut -c 10- passwd
实验测试:显示网卡IP
[root@shell_1 test]# ifconfig ens3| head -n2 |tail -n1 | cut -d ' ' -f 10
172.25.254.150
[root@shell_1 test]# ifconfig ens3 | awk '/inet\>/{print$2}'
172.25.254.150
四、sort
-n | 纯数字排序 |
-r | 倒序排列 |
-u | 去掉重复 |
-o | 输出到指定文件 |
-t | 指定分隔符 |
-k | 指定排序的列 |
五、uniq
-c | 合并重复并统计重复个数 |
-d | 显示重复的行 |
-u | 显示唯一的行 |
六、tr
tr 'a-z' 'A-Z' < westos | westos文件中小写转大写 |
tr 'A-Z' ‘a-z' < westos | westos文件中大写转小写 |
tr 'y' 'n' < westos | westos文件中的字符y换成字符n |
七、test
test = [ ] # [ ] 就相当于test命令 “test $a = $b” = [ "$a" = "$b" ]
test命令数字对比
= | 等于 | -eq | 等于 |
!= | 不等于 | -ne | 不等于 |
-lt | 小于 | -gt | 大于 |
-le | 小于等于 | -ge | 大于等于 |
test的条件关系
-a | 并且 |
-o | 或者 |
test对空的判定
-n | #nozero,判定内容不为空 |
-z | #zero , 判定内容为空 |
test对文件的判定
-ef | 文件节点号是否一致(硬链接) | -d | 目录 |
-nt | 文夹1是不是比文件2新 | -S | 套接字 |
-ot | 文件1是不是比文件2老 | -L | 软链接 |
-e | 存在 | -f | 普通文件 |
-b | 块设备 | -c | 字符设备 |
练习:
本章总结:
cut -d "a" -f 10
cut命令:对列分割时,引号内为分隔符。就是将内容以“a”为标准分隔,a前和a后分别为两列