从MANIFEST.MF文件中导出Grep Exported-Packages

时间:2021-10-31 06:28:44

I need help to grep the exported packages from an OSGi MANIFEST.MF file. For example, from the following file:

我需要帮助来从OSGi MANIFEST.MF文件中grep导出的包。例如,从以下文件:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: MyBundle
Bundle-SymbolicName: mybundle
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework;version="1.6.0",
 org.osgi.util.tracker;version="1.3.1"
Export-Package: foo.bar.bla,
 foo.bar.blo,
 foo.bor.ble,
 foo.bor.bli
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Activator: foo.bar.bla.Activator

I need to get:

我需要得到:

foo.bar.bla
foo.bar.blo
foo.bor.ble
foo.bor.bli

We must have in mind that the following tag after the last package could be Bundle-RequiredExecutionEnvironmen or Bundle-Activator, or any other.

我们必须记住,最后一个包之后的以下标记可以是Bundle-RequiredExecutionEnvironmen或Bundle-Activator,或任何其他。

EDIT: With:

cat MANIFEST.MF | awk '/Export-Package:(.*)/ {print $2; flag=1} flag=1 && /:/ {flag=0;next } flag=1 {print}' | sed 's/,//g'

I'm getting:

 org.osgi.util.tracker;version="1.3.1"
foo.bar.bla
 foo.bar.blo
 foo.bor.ble
 foo.bor.bli

It shouldn't retrieve org.osgi.util.tracker;version="1.3.1"

它不应该检索org.osgi.util.tracker; version =“1.3.1”

1 个解决方案

#1


1  

I'd recommend you use bnd.

我建议你使用bnd。

  • install bnd
  • Execute bnd select --header Export-Package my.jar
  • 执行bnd select --header Export-Package my.jar

And all the edge cases are handled for you.

所有边缘案例都会为您处理。


Old answer for people interested in "using the command line" aspect of the question.

对于对“使用命令行”方面感兴趣的人的老答案。

This is not a job for Grep as grep is a line-by-line sort of thing, this is a job for awk or sed!

这不是Grep的工作,因为grep是一个逐行排序的东西,这是awk或sed的工作!

echo "Export-Package: foo.bar.bla,
    foo.bar.blo,
    foo.bor.ble,
    foo.bor.bli
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
" | cat MANIFEST.MF | awk '/Export-Package:(.*)/ {print $2; flag=1; next} flag && /:/ {flag=0;next} flag {print}' | sed 's/,//g'
  • /Export-Package:(.*)/ Find the line "Export-Package" and capture what ever is after ":".
  • /Export-Package:(.*)/找到“Export-Package”行并捕获“:”之后的内容。

  • {print $2; flag=1; next} Print out the captured group and set a flag to true. Skip the rest of the rule for this line
  • {print $ 2;标志= 1; next}打印出捕获的组并将标志设置为true。跳过此行的其余规则

  • flag && /:/ When the flag is set, and the line contains ":"
  • flag && /:/设置标志后,该行包含“:”

  • {flag=0;next } Set the flat to false and skip the rest of the rules for this line
  • {flag = 0; next}将flat设置为false并跳过此行的其余规则

  • flag If the flag is true
  • flag如果标志为true

  • {print} Print out the line.
  • {print}打印出该行。

However this does not cover edge cases such as word wraps (thanks Neil Bartlett), while we could make the awk program more complex this is a waste of time when someone else has created an application to do just that (thanks BJ Hargrave).

然而,这并不包括诸如自动换行的边缘情况(感谢Neil Bartlett),而我们可以使awk程序更复杂,这是浪费时间,而其他人创建了一个应用程序来做到这一点(感谢BJ Hargrave)。

#1


1  

I'd recommend you use bnd.

我建议你使用bnd。

  • install bnd
  • Execute bnd select --header Export-Package my.jar
  • 执行bnd select --header Export-Package my.jar

And all the edge cases are handled for you.

所有边缘案例都会为您处理。


Old answer for people interested in "using the command line" aspect of the question.

对于对“使用命令行”方面感兴趣的人的老答案。

This is not a job for Grep as grep is a line-by-line sort of thing, this is a job for awk or sed!

这不是Grep的工作,因为grep是一个逐行排序的东西,这是awk或sed的工作!

echo "Export-Package: foo.bar.bla,
    foo.bar.blo,
    foo.bor.ble,
    foo.bor.bli
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
" | cat MANIFEST.MF | awk '/Export-Package:(.*)/ {print $2; flag=1; next} flag && /:/ {flag=0;next} flag {print}' | sed 's/,//g'
  • /Export-Package:(.*)/ Find the line "Export-Package" and capture what ever is after ":".
  • /Export-Package:(.*)/找到“Export-Package”行并捕获“:”之后的内容。

  • {print $2; flag=1; next} Print out the captured group and set a flag to true. Skip the rest of the rule for this line
  • {print $ 2;标志= 1; next}打印出捕获的组并将标志设置为true。跳过此行的其余规则

  • flag && /:/ When the flag is set, and the line contains ":"
  • flag && /:/设置标志后,该行包含“:”

  • {flag=0;next } Set the flat to false and skip the rest of the rules for this line
  • {flag = 0; next}将flat设置为false并跳过此行的其余规则

  • flag If the flag is true
  • flag如果标志为true

  • {print} Print out the line.
  • {print}打印出该行。

However this does not cover edge cases such as word wraps (thanks Neil Bartlett), while we could make the awk program more complex this is a waste of time when someone else has created an application to do just that (thanks BJ Hargrave).

然而,这并不包括诸如自动换行的边缘情况(感谢Neil Bartlett),而我们可以使awk程序更复杂,这是浪费时间,而其他人创建了一个应用程序来做到这一点(感谢BJ Hargrave)。