使用sed从`gem environment`的输出中提取路径

时间:2015-04-29 03:27:50

标签: regex sed gem extraction

执行gem environment会返回以下输出:

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
  - INSTALLATION DIRECTORY: /var/lib/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /var/lib/gems/1.8
     - /root/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

使用sed,如何专门提取EXECUTABLE DIRECTORY值以检索/var/lib/gems/1.8/bin

2 个答案:

答案 0 :(得分:1)

您可以使用输出管道输出为awk:

gem environment | awk -F ' *: *' '$1 ~ /EXECUTABLE DIRECTORY/{print $2}'
/var/lib/gems/1.8/bin

答案 1 :(得分:0)

我认为使用sed真的很难,你基本上必须删除不包含EXECUTABLE DIRECTORY的每一行,而从其他行删除一切直到冒号。

gem environment | sed '/EXECUTABLE DIRECTORY/!d;s/^.*: //'

我会为此推荐awk

gem environment | awk -F': *' '/EXECUTABLE DIRECTORY/ {print $2}'

grep

gem environment |  grep -Po 'EXECUTABLE DIRECTORY: \K.*'