【问题标题】:Single Column rpm command output单列 rpm 命令输出
【发布时间】:2013-07-03 12:32:38
【问题描述】:

我有一个应用程序,我想在其中获取几个包的 RPM 信息并以令人愉悦和引人注目的方式对其进行格式化。 *繁荣*。

我的问题是rpm想把数据格式化成两列,很烦。

命令

rpm -qi ruby

结果

Name        : ruby                         Relocations: (not relocatable)
Version     : 1.8.6.36                          Vendor: (none)
Release     : 2                             Build Date: Thu 18 Sep 2008 10:50:30 AM EDT
Install Date: Thu 25 Sep 2008 12:12:53 PM EDT      Build Host: kickbench
Group       : Development/Languages         Source RPM: ruby-1.8.6.36-2.src.rpm
Size        : 1664035                          License: Ruby License/GPL - see COPYING
Signature   : (none)
URL         : http://www.ruby-lang.org/
Summary     : An interpreter of object-oriented scripting language.
Description : [truncated]

我想要什么

Name        : ruby
Relocations : (not relocatable)
Version     : 1.8.6.36
Vendor      : (none)
Release     : 2
Build Date  : Thu 18 Sep 2008 10:50:30 AM EDT
Install Date: Thu 25 Sep 2008 12:12:53 PM EDT
Build Host  : kickbench
Group       : Development/Languages
Source RPM  : ruby-1.8.6.36-2.src.rpm
Size        : 1664035
License     : Ruby License/GPL - see COPYING
Signature   : (none)
URL         : http://www.ruby-lang.org/
Summary     : An interpreter of object-oriented scripting language.
Description : [truncated]

有没有办法在不手动指定真正长的--queryformat 的情况下获得这种格式?

或者,任何方法都可以在 Ruby 中执行此操作。

【问题讨论】:

    标签: ruby linux terminal


    【解决方案1】:
    pairs = %x{rpm -qi ruby}
           .split(/(?<!:)\s{2,}(?![\s:])|#$//)
           .map{|line| line.split(/\s*:\s+/, 2)}
    width = pairs.map{|pair| pair.first.length}.max
    pairs.each{|k, v| puts "#{k.ljust(width)}: #{v}"}
    

    【讨论】:

    • 好吧。这并不漂亮,但它回答了这个问题。我决定使用查询格式。
    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2014-09-09
    相关资源
    最近更新 更多