比较两个文件并以 table 形式 linux shell 脚本显示差异

Compare two files and display difference in table form linux shell script

我正在尝试比较两个文件的差异,这两个文件是更新前后的软件包列表,并以易于阅读的形式显示它。

基本上我的一些内容是这样的:

更新前:

2:tar-1.23-13.el6.x86_64/
tcp_wrappers-libs-7.6-57.el6.x86_64/
14:tcpdump-4.0.0-5.20090921gitdf3cb4.2.el6.x86_64/
3:traceroute-2.0.14-2.el6.x86_64/

Post更新:

2:tar-1.23-15.el6_8.x86_64/
tcp_wrappers-libs-7.6-57.el6.x86_64/
14:tcpdump-4.0.0-5.20090921gitdf3cb4.2.el6.x86_64/
3:traceroute-2.0.14-2.el6.x86_64/
samba-common-3.6.23-43.el6_9.x86_64/
samba-winbind-clients-3.6.23-43.el6_9.x86_64/
samba-winbind-3.6.23-43.el6_9.x86_64/

预期输出:

Pre-Update                |             Postupdate
2:tar-1.23-13.el6.x86_64/ | 2:tar-1.23-15.el6_8.x86_64/
(empty) | samba-common-3.6.23-43.el6_9.x86_64/
(empty) | samba-winbind-clients-3.6.23-43.el6_9.x86_64/
(empty) | samba-winbind-3.6.23-43.el6_9.x86_64/

基本上在两个文件下显示更新,仅在新文件下显示添加。

我不介意其他方式来显示它,只要它的格式很好,例如 oldpackagename --> newpackagename 或沿该路径的其他内容。

如果你想要漂亮的并排输出,你可以使用:

$ diff -y --suppress-common-lines file1.txt file2.txt

例子Use/Output

$ diff -y --suppress-common-lines file1.txt file2.txt
2:tar-1.23-13.el6.x86_64/                 | 2:tar-1.23-15.el6_8.x86_64/
                                          > samba-common-3.6.23-43.el6_9.x86_64/
                                          > samba-winbind-clients-3.6.23-43.el6_9.x86_64/
                                          > samba-winbind-3.6.23-43.el6_9.x86_64/

找到了一种非常简单的方法,只需一行代码即可完成此操作:

echo "Pre-Upgrade | Post-Upgrade " > test.txt; diff -y --suppress-common-lines filea.txt fileb.txt >> test.txt

输出: