使用sed组合两条特定行 [英] Combine two particular lines using sed
问题描述
我有以下输入文件,您可以将其识别为debian 包
文件:
I have the following input file that you might recognize as a debian Packages
file:
Package: nimbox-apexer-sales
Version: 1.0.0-201007241449
Architecture: i386
Maintainer: Ricardo Marimon <rmarimon@nimbox.com>
Installed-Size: 124
Depends: nimbox-apexer-root
Filename: binary/nimbox-apexer-sales_1.0.0-201007241449_i386.deb
Size: 68880
MD5sum: c4538f2913d76b57110ba73d0b87cc16
Section: base
Priority: optional
Description: Sales Application for NiMbox.
Package: nimbox-tomcat
Version: 6.0.26-5
Architecture: i386
Maintainer: Ricardo Marimon <rmarimon@nimbox.com>
Installed-Size: 6144
Depends: sun-java6-jdk
Filename: binary/nimbox-tomcat_6.0.26-5_i386.deb
Size: 5490024
MD5sum: 5f2ccbe6137af2842e1c81bc217444e3
Section: base
Priority: optional
Description: Tomcat Servlet Application Server for NiMbox
NiMbox requires a servlet application server in order to work. The current
NiMbox implementation requires a Tomcat Servlet Application.
该文件实际上有许多这些条目,我想获取以下文件$ / b
$ b
The file actually has many of these entries and I want to get the following file
nimbox-apexer-sales 1.0.0-201007241449
nimbox-tomcat 6.0.26-5
包
和版本
由选项卡
分隔,以便稍后使用 cut
来获取它们。我很确定这可以用 sed
完成。我去过一个衬衫,但这可能有点复杂了。任何想法?
Where the Package
and the Version
are separated by a tab
so that I can later use cut
to get them. I'm pretty sure this can be done with sed
. I went over the sed one liners but this is probably a bit more complex. Any ideas?
推荐答案
使用Debian Packages文件时,您可能会发现 grep-dctrl
有用。它允许限制
输出的数据以及如何输出它的方式非常灵活。而不是试图自行解析Packages
文件格式,我只需要求 grep-dctrl
为我做,而只打印
如果我真正感兴趣的信息是:
When working with Debian Packages files, you might find grep-dctrl
useful. It's incredibly flexible in both the ways it allows to limit the data it
outputs, as well as in how to output it. Instead of trying to parse the Packages
file format myself, I'd just ask grep-dctrl
to do it for me, and print only
the bits if information I'm actually interested in:
$ grep-dctrl -n -s Package,Version nimbox /var/lib/apt/lists/..._Packages
这将给你一些像:
nimbox-apexer-sales
1.0.0-201007241449
nimbox-tomcat
6.0.26-5
与此相反,这只是一起加入正确的一行,这很容易
足够,例如perl:
With that, it's only a matter of joining the right lines together, which is easy enough with, for example, perl:
$ ... |perl -pi -0e's/(?<!^)\n(?!\n)/ /mg; s/\n\n/\n/g'
nimbox-apexer-sales 1.0.0-201007241449
nimbox-tomcat 6.0.26-5
或任何您喜欢的其他标准UNIX工具集。
or any set of other standard UNIX tools you happen to like.
可能直接从Packages文件格式转到
想要的东西,但使用专门针对该工作的工具似乎是一个好主意。
It's certainly possible to go directly from the Packages file format to what you want, but using tools specialized for the job seems like a good idea to me.
这篇关于使用sed组合两条特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!