Linux shell脚本比较两个文件并将差异写入第三个文件
我想比较两个文件,并将两个文件之间的差异重定向到第三个文件。Linux shell脚本比较两个文件并将差异写入第三个文件,linux,shell,Linux,Shell,我想比较两个文件,并将两个文件之间的差异重定向到第三个文件。 文件1: 如果任何文件在/opt/c/c.sql之前有,则应跳过 文件2: /opt/c/c.sql /opt/a/a.sql 我想知道这两个文件之间的区别。在这种情况下,/opt/b/b.sql应存储在不同的文件中。有人能帮我实现上述方案吗?file1 $ cat file1 #both file1 and file2 may contain spaces which are ignored /opt/a/a.sql /op
文件1: 如果任何文件在
/opt/c/c.sql
之前有
,则应跳过
文件2:
/opt/c/c.sql
/opt/a/a.sql
我想知道这两个文件之间的区别。在这种情况下,/opt/b/b.sql
应存储在不同的文件中。有人能帮我实现上述方案吗?file1
$ cat file1 #both file1 and file2 may contain spaces which are ignored
/opt/a/a.sql
/opt/b/b.sql
/opt/c/c.sql
/opt/h/m.sql
文件2
$ cat file2
/opt/c/c.sql
/opt/a/a.sql
Do
awk 'NR==FNR{line[$1];next}
{if(!($1 in line)){if($0!=""){print}}}
' file2 file1 > file3
file3
$ cat file3
/opt/b/b.sql
/opt/h/m.sql
注意事项:
file2
此处-首先是主文件-file1
awk
以了解此处的操作您可以使用一些工具,如
cat
、sed
、sort
和uniq
主要观察结果是:如果该行在两个文件中,那么它在cat file1 file2
中不是唯一的
此外,在cat file1 file2 | sort
中,所有的双精度字符都是按顺序排列的。使用uniq-u
我们可以得到唯一的管线,并拥有以下管道:
cat file1 file2 | sort | uniq -u
使用sed
删除前导空格、空行和注释行,我们得到最后一个管道:
cat file1 file2 | sed -r 's/^[ \t]+//; /^#/ d; /^$/ d;' | sort | uniq -u > file3
首先,您应该决定要使用三种脚本中的哪一种:batch、powershell或shell scriptSecond,您应该研究选择脚本语言时使用的命令。我尝试了diff file file2,但没有得到预期的结果,我只需要/opt/b/b.sql作为输出。可能吗?如果可能的话,任何人都可以帮助我。。。
cat file1 file2 | sed -r 's/^[ \t]+//; /^#/ d; /^$/ d;' | sort | uniq -u > file3