加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在一个文件中找到行,而不是使用bash脚本查找其他文件?

发布时间:2020-12-15 19:06:26 所属栏目:安全 来源:网络整理
导读:想象文件1: #include "first.h"#include "second.h"#include "third.h"// more code here... 想象文件2: #include "fifth.h"#include "second.h"#include "eigth.h"// more code here... 我想得到文件2中包含的标题,但不包含在文件1中的标题,只有这些行
想象文件1:
#include "first.h"
#include "second.h"
#include "third.h"

// more code here
...

想象文件2:

#include "fifth.h"
#include "second.h"
#include "eigth.h"

// more code here
...

我想得到文件2中包含的标题,但不包含在文件1中的标题,只有这些行。
所以,当运行时,文件1和文件2的差异会产生:

#include "fifth.h"
#include "eigth.h"

我知道如何在Perl / Python / Ruby中执行此操作,但是我想在不使用不同的编程语言的情况下完成此操作。

如果可以使用临时文件,请尝试:
grep include file1.h > /tmp/x && grep -f /tmp/x -v file2.h | grep include

这个

>从file1.h中提取所有文件,并将它们写入文件/ tmp / x
>使用此文件来获取file2.h中不包含在此列表中的所有行
> extract中包含的所有文件都包含在file2.h的其余部分中

它可能不会正确地处理空白的差异等。

编辑:
为了防止误报,对最后一个grep使用不同的模式(感谢jw013提到这一点):

grep include file1.h > /tmp/x && grep -f /tmp/x -v file2.h | grep "^#include"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读