我有两个包含依赖项列表的大文本文件。
第一个文件包含按服务分类的项目中的依赖项列表,该文件的一小段如下所示:
project_dependencies.txt:
------------- Dependencies for: Service1 -------------
org.springframework:spring-context:4.3.16.RELEASE
org.springframework:spring-aop:4.3.16.RELEASE
------------- Dependencies for: Service2 -------------
org.jongo:jongo:1.4.0
org.apache.commons:commons-lang3:3.4
javax.servlet:javax.servlet-api:3.1.0
------------- Dependencies for: Service3-------------
junit:junit:4.12
org.springframework:spring-context:4.3.16.RELEASE
...
第二个包含我公司批准的依赖项列表。看起来很相似,但是没有服务。这是一个片段:
approved_dependencies.txt:
...
com.google.code.findbugs:annotations:2.0.1
com.google.code.findbugs:bcel:2.0.1
com.google.code.findbugs:bcel-findbugs:6
com.google.code.findbugs:findbugs:3.0.1
com.google.code.findbugs:findbugs-ant:2.0.1
com.google.code.findbugs:jFormatString:2.0.1
com.google.code.findbugs:jsr305:2.0.1
com.google.code.gson:gson:2.3.1
com.google.code.p.arat:rat-lib:0.5.1
org.springframework:spring-context:4.3.16.RELEASE
...
我正在寻找一个bash命令,甚至是一个awk
脚本,该命令可以比较这两个文件,并且基本上输出一个看起来非常类似于第一个文件的文件(project_dependencies.txt),但仅包含依赖项在第二个文件中,但仍按服务排序。我一直在尝试使用grep
进行此操作,但尚未找到解决方案。我该怎么用?
为了更清楚一点,这是所需输出文件中的示例片段:
...
------------- Dependencies for: Service3 -------------
org.springframework:spring-context:4.3.16.RELEASE
------------- Dependencies for: Service4 -------------
com.google.code.findbugs:bcel:2.0.1
com.google.code.findbugs:bcel-findbugs:6
------------- Dependencies for: Service5-------------
com.google.code.findbugs:jsr305:2.0.1
com.google.code.gson:gson:2.3.1
com.google.code.p.arat:rat-lib:0.5.1
...
为简洁起见,此代码段中未实际表示每个服务下的依赖项的实际数字。我要在此处显示的所有内容是,不在原始列表中的approved_dependencies.txt
中没有的依赖项已被删除,并且服务的继续(在本例中为Service4和service5)仅包含批准的依赖项摘录中的依赖项。 / p>
请让我知道我是否有任何遗漏,太含糊或需要在评论中进行澄清。
答案 0 :(得分:2)
这些要求似乎使它们适用于grep的简单应用程序:
grep -F -f approved_dependencies.txt -e ---- project_dependencies.txt
-F
-搜索固定字符串而不是正则表达式-f file
-查找file
-e ----
-还查找带有多个连字符的行(或者,如果您想更具体一些,可以使用'Dependencies for:'
或类似的字符)答案 1 :(得分:1)
您的示例输出所包含的服务不在您的示例输入中,这使测试变得困难,但也许:
$ awk -F: 'NR == FNR { deps[$1,$2] = $3; next }
$0 ~ /^---/ { print }
($1,$2) in deps && deps[$1,$2] == $3 { print }' approved_dependencies.txt project_dependencies.txt
------------- Dependencies for: Service1 -------------
org.springframework:spring-context:4.3.16.RELEASE
------------- Dependencies for: Service2 -------------
------------- Dependencies for: Service3-------------
org.springframework:spring-context:4.3.16.RELEASE