ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-04-13 23:12:23  阅读:190  来源: 互联网

标签:tmp file1 shell -- 文本处理 txt root localhost


文本处理工具--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]#

标签:tmp,file1,shell,--,文本处理,txt,root,localhost
来源: https://blog.51cto.com/u_13625527/2704422

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有