从0开始学习shell--文本处理工具diff

2021/4/14 7:27:28

本文主要是介绍从0开始学习shell--文本处理工具diff,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文本处理工具--diff
怎么改变第一个文件和第二个文件匹配

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

正常模式
[root@localhost tmp]# cat -n file1.txt
1 aaaa
2 111
3 helloworld
4 222
5 333
6 bbb
[root@localhost tmp]# cat -n file2.txt
1 aaa
2 hello
3 111
4 222
5 bbb
6 333
7 world
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# diff file1.txt
diff: missing operand after 'file1.txt'
diff: Try 'diff --help' for more information.
[root@localhost tmp]# diff file1.txt file2
diff: file2: No such file or directory
[root@localhost tmp]# diff file1.txt file2.txt
1c1,2 C--代表change
< aaaa

aaa
hello
3d3 d--代表删除
< helloworld
5d4
< 333
6a6,7 a--代表增加
333
world
[root@localhost tmp]#

Diff 上下文格式 -c

[root@localhost tmp]# diff -c file1.txt file2.txt
*** file1.txt 2021-04-13 22:19:23.837567584 +0800
--- file2.txt 2021-04-13 22:19:55.026974946 +0800


* 1,6 **
! Aaaa 改变
111

  • helloworld 删除
    222
  • 333
    bbb
    --- 1,7 ----
    ! aaa
    ! hello
    111
    222
    bbb
  • 333 增加
  • world
    [root@localhost tmp]#
    -u 合并模式

[root@localhost tmp]# diff -u file1.txt file2.txt
--- file1.txt 2021-04-13 22:19:23.837567584 +0800
+++ file2.txt 2021-04-13 22:19:55.026974946 +0800
@@ -1,6 +1,7 @@
-aaaa
+aaa
+hello
111
-helloworld
222
-333
bbb
+333
+world
[root@localhost tmp]#

比较目录里面的内容不同
[root@localhost tmp]# ll -R dir*
dir1:
total 0
-rw-r--r-- 1 root root 0 Apr 13 22:32 file1
-rw-r--r-- 1 root root 0 Apr 13 22:32 file2
-rw-r--r-- 1 root root 0 Apr 13 22:32 file3
-rw-r--r-- 1 root root 0 Apr 13 22:32 file4
-rw-r--r-- 1 root root 0 Apr 13 22:32 file5

dir2:
total 0
-rw-r--r-- 1 root root 0 Apr 13 22:32 file1
-rw-r--r-- 1 root root 0 Apr 13 22:32 file2
-rw-r--r-- 1 root root 0 Apr 13 22:32 file3
-rw-r--r-- 1 root root 0 Apr 13 22:33 test1
-rw-r--r-- 1 root root 0 Apr 13 22:33 test2
[root@localhost tmp]# diff -q dir1 dir2
Only in dir1: file4
Only in dir1: file5
Only in dir2: test1
Only in dir2: test2

打补丁的方式
(1)找出文件的不同,然后输出到一个文件中
(2)将不同内容打补丁到文件
(3)测试验证

[root@localhost tmp]# cat file1.txt
aaaa
111
helloworld
222
333
bbb
[root@localhost tmp]# cat file2.txt
aaa
hello
111
222
bbb
333
world
[root@localhost tmp]# patch file1.txt pa
paramiko/ passwd
[root@localhost tmp]# patch file1.txt pa
paramiko/ passwd
[root@localhost tmp]# diff -u file1.txt file2.txt > patch.txt
[root@localhost tmp]# cat patch.txt
--- file1.txt 2021-04-13 22:19:23.837567584 +0800
+++ file2.txt 2021-04-13 22:19:55.026974946 +0800
@@ -1,6 +1,7 @@
-aaaa
+aaa
+hello
111
-helloworld
222
-333
bbb
+333
+world
[root@localhost tmp]# patch file1.txt pa
paramiko/ passwd patch.txt
[root@localhost tmp]# patch file1.txt pa
paramiko/ passwd patch.txt
[root@localhost tmp]# patch file1.txt patch.txt
patching file file1.txt
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# diff -u file1.txt file2.txt
[root@localhost tmp]#



这篇关于从0开始学习shell--文本处理工具diff的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程