如何在linux中比较两个乳胶文件?

时间:2014-07-28 08:28:33

标签: linux bash awk sed

我需要将New.tex http://pastebin.com/PCafGYwG与Old.tex http://pastebin.com/jRU8rhhV进行比较,如果同一IP中的两个文档中存在相同的漏洞,则需要输出。

例如:两个文件的IP地址为10.1.2.3。

New.tex

 \subsection {Summary of security vulnerabilities found on the host with IP address 10.1.2.3}
\begin{center}
\begin{longtable}{|p{0.2in}|p{0.5in}|p{4in}|p{0.4in}|} \hline
\textit{\textbf{No}} &\textit{\textbf{Severity level}} & \textit{\textbf{Vulnerability name}} & \textit{\textbf{More}} \\ \hline
     1  & \cellcolor{Red}High& DNS Server Cache Snooping Remote Information Disclosure  & \href{http://www.rootsecure.net/content/downloads/pdf/dns\_cache\_snooping.pdf}{$\Rightarrow$}  \\ \hline
     2  & \cellcolor{BurntOrange}Medium & SSL Certificate with Wrong Hostname  & \href{http://www.tenable.com/plugins/index.php?view=single&id=45411}{$\Rightarrow$}  \\ \hline
     3  & \cellcolor{BurntOrange}Medium & SSL Self-Signed Certificate  & \href{http://www.tenable.com/plugins/index.php?view=single&id=57582}{$\Rightarrow$}  \\ \hline
     4  & \cellcolor{SkyBlue}Low & SSL RC4 Cipher Suites Supported  & \href{http://www.nessus.org/u?217a3666}{$\Rightarrow$}  \\
\end{longtable}
\end{center}
.............
.....................
...

Old.tex

.............
.......
......................
 \subsection {Summary of security vulnerabilities found on the host with IP address 10.1.2.3}
\begin{center}
\begin{longtable}{|p{0.2in}|p{0.5in}|p{4in}|p{0.4in}|} \hline
\textit{\textbf{No}} &\textit{\textbf{Severity level}} & \textit{\textbf{Vulnerability name}} & \textit{\textbf{More}} \\ \hline
     1  & \cellcolor{BurntOrange}Medium & DNS Server Cache Snooping Remote Information Disclosure  & \href{http://www.rootsecure.net/content/downloads/pdf/dns\_cache\_snooping.pdf}{$\Rightarrow$}  \\ \hline
     2  & \cellcolor{BurntOrange}Medium & SSL Certificate Cannot Be Trusted  & \href{http://www.tenable.com/plugins/index.php?view=single&id=51192}{$\Rightarrow$}  \\ \hline
     3  & \cellcolor{BurntOrange}Medium & SSL Certificate with Wrong Hostname  & \href{http://www.tenable.com/plugins/index.php?view=single&id=45411}{$\Rightarrow$}  \\ \hline
     4  & \cellcolor{BurntOrange}Medium & SSL Self-Signed Certificate  & \href{http://www.tenable.com/plugins/index.php?view=single&id=57582}{$\Rightarrow$}  \\ \hline
\end{longtable}
\end{center}
..............
.......
...........

在那种情况下,

都有漏洞=“DNS服务器缓存侦听远程信息泄露”“具有错误主机名的SSL证书”“SSL自签名证书”

我需要输出

仅漏洞摘要如下

\subsection {Summary of security vulnerabilities found on the host with IP address 10.1.2.3}
\begin{center}
\begin{longtable}{|p{0.2in}|p{0.5in}|p{4in}|p{0.4in}|} \hline
\textit{\textbf{No}} &\textit{\textbf{Severity level}} & \textit{\textbf{Vulnerability name}} & \textit{\textbf{More}} \\ \hline
     1  & \cellcolor{Red}Medium & DNS Server Cache Snooping Remote Information Disclosure  & \href{http://www.rootsecure.net/content/downloads/pdf/dns\_cache\_snooping.pdf}{$\Rightarrow$}  \\ \hline
     2  & \cellcolor{BurntOrange}Medium & SSL Certificate with Wrong Hostname  & \href{http://www.tenable.com/plugins/index.php?view=single&id=45411}{$\Rightarrow$}  \\ \hline
     3  & \cellcolor{BurntOrange}Medium & SSL Self-Signed Certificate  & \href{http://www.tenable.com/plugins/index.php?view=single&id=57582}{$\Rightarrow$}  \\ \hline
\end{longtable}
\end{center}

如何使用bash脚本,awk,sed或任何其他方法在linux上实现此目的?

2 个答案:

答案 0 :(得分:0)

您可以使用diff命令或diffuse GUI工具比较两个文件:

diff Old.tex New.tex

diffuse Old.tex New.tex

要进行更深入的分析,我建议您编写一个简短的python(或perl)脚本,在这里您可以读取这两个文件,找到您感兴趣的行,进行比较,并进行必要的替换。

答案 1 :(得分:-1)

最简单的方法是在第一行中找到要搜索的唯一文本,然后使用grep找到该行并添加-A8以捕获接下来的8行上下文。在您的情况下,可能会出现以下情况:

grep -A8 {Summary\ of\ security\ vulnerabilities filename

假设单词组合对于漏洞行是唯一的,那么您将得到您指出的上述行。