以下是gem environment
命令的输出。
○ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.23
- RUBY VERSION: 1.9.3 (2012-04-20 patchlevel 194) [x86_64-darwin10.8.0]
- INSTALLATION DIRECTORY: /Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
- RUBY EXECUTABLE: /Users/userme/.rbenv/versions/1.9.3-p194/bin/ruby
- EXECUTABLE DIRECTORY: /Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-10
- GEM PATHS:
- /Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
- .bundle
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://rubygems.org/"]
- "gem" => "--no-rdoc --no-ri"
- REMOTE SOURCES:
- http://rubygems.org/
userme at userme in ~
awk
命令用于捕获/Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
在- GEM PATHS:
行前-
下的/Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
行?
答案 0 :(得分:2)
尝试:
$ gem env gempath
/home/mpapis/.rvm/gems/ruby-1.9.3-p286:/home/mpapis/.rvm/gems/ruby-1.9.3-p286@global
它将显示用PATH_SEPARATOR分隔的所有路径,在linux上为:
答案 1 :(得分:2)
使用awk
,您可以通过以下方式输出输出:
awk '{if(e){sub (/^ *- */,"",$0);print;e=0}}/GEM PATHS/{e=1}'
它的工作原理如下:
e
(echo)标志从0开始,因此禁用了回显。<spaces>-<spaces>
,回显修改后的行,然后重置标志。GEM PATHS
行,则会设置该标志,以便使用上一个规则输出下一行。以下是此行动的一个小成绩单:
pax> echo 'RubyGems Environment:
- RUBYGEMS VERSION: 1.8.23
- GEM PATHS:
- /Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
- .bundle
- GEM CONFIGURATION:
- :update_sources => true
' | awk '{if(e){sub (/^ *- */,"",$0);print;e=0}}/GEM PATHS/{e=1}'
/Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global
答案 2 :(得分:2)
这是使用sed
的方式:
sed -n '/- GEM PATHS:/ { N; s/.* - //p }' file
另一种使用awk
的方式,如果您熟悉getline
的使用:
awk '/- GEM PATHS:/ { getline; sub(/.* - /,""); print }' file
否则,这可能是你最安全的选择:
awk 'n && !--n { sub(/.* - /,""); print } /- GEM PATHS:/ { n = 1 }' file
结果:
/Users/userme/.rbenv/versions/1.9.3-p194/gemsets/global