使用 sed 从多行中提取带引号的值

发布于 2024-10-04 14:26:02 字数 1126 浏览 4 评论 0原文

当谈到使用 sed 时,我完全不懂,并且无法完全弄清楚这一点...

我得到了由命令行工具输出的以下数据:

ruby:
interpreter:  "ruby"
version:      "1.8.6"
date:         "2010-02-04"
platform:     "i386-mingw32"
patchlevel:   "398"
full_version: "ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]"

homes:
gem:          ""
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32"

binaries:
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin"
irb:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\irb.bat"
gem:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\gem.bat"
rake:         ""

environment:
GEM_HOME:     ""
HOME:         "c:/Users/derick"
IRBRC:        ""
RUBYOPT:      ""

file associations:
.rb:
.rbw:

我想使用 sed仅从 interpreterversion 中提取值,并将它们作为“interpreter-version”返回。

到目前为止我能想到的最好的方法是使用 grep 返回整个版本行:

pik info | grep -e '^version:.*'

但这从版本行返回太多信息,并且不包括解释器(显然)。

如何使用 sed 只提取我想要的信息位,以便获得 ruby​​-1.8.6 的结果?

...或grep,或awk,或任何其他命令行工具来获取我想要的信息...

FWIW:我没有使用RVM,因为这是一台使用MinGW32的Windows机器...我正在使用Pik,它与 RVM 类似,但专为 Windows 设计。

I'm a total n00b when it comes to using sed and can't quite figure this out...

I've got the following data being output by a command line tool:

ruby:
interpreter:  "ruby"
version:      "1.8.6"
date:         "2010-02-04"
platform:     "i386-mingw32"
patchlevel:   "398"
full_version: "ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]"

homes:
gem:          ""
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32"

binaries:
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin"
irb:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\irb.bat"
gem:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\gem.bat"
rake:         ""

environment:
GEM_HOME:     ""
HOME:         "c:/Users/derick"
IRBRC:        ""
RUBYOPT:      ""

file associations:
.rb:
.rbw:

I want to use sed to extract the values from interpreter and version only, and have them returned as "interpreter-version".

The best I could come up with so far is to use grep to return the entire version line:

pik info | grep -e '^version:.*'

but that returns too much info from the version line, and doesn't include the interpreter (obviously).

How can i use sed to extract only the bits of information that i want, so that i get a result of ruby-1.8.6?

... or grep, or awk, or any other command line tool to get the info i want...

FWIW: i'm not using RVM because this is a windows machine using MinGW32... i'm using Pik, which is similar to RVM but made for windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

预谋 2024-10-11 14:26:02

你们中的很多人都在努力处理报价。让 awk 来做:

pik info | awk -F '"' '
    /^interpreter:/ {interp = $2} 
    /^version:/     {ver = $2}
    interp && ver   {print interp "-" ver; exit}
'

A lot of you are working hard to deal with the quotes. Let awk do it:

pik info | awk -F '"' '
    /^interpreter:/ {interp = $2} 
    /^version:/     {ver = $2}
    interp && ver   {print interp "-" ver; exit}
'
可是我不能没有你 2024-10-11 14:26:02

使用“full_version”字符串有用吗?

pik info | awk '/full_version/ {print $2,$3}' | sed 's/\"//;s/\s/\-/'

我确信 sed 位可以用“sub()”折叠到 awk 位中,但我会将其留给更精通 awk 的人。

Would working with the 'full_version' string be useful?

pik info | awk '/full_version/ {print $2,$3}' | sed 's/\"//;s/\s/\-/'

I'm certain the sed bit could be folded into the awk bit with 'sub()', but I'll leave it to someone more versed in awk.

合久必婚 2024-10-11 14:26:02

如果您知道“保留空间”,那么在纯“sed”中就很容易做到:

sed -n '/^interpreter: *"\(.*\)"/{
s//\1/
h
}
/^version: *"\(.*\)"/{
s// \1/
H
x
s/\n//
s/.*/interpreter-version: "&"/p
}'

除非您命令它,否则脚本总体上不会打印任何内容(-n)。搜索的第一个模式是解释器行;我们捕获解释器的名称,然后删除所有其余的材料,并将解释器名称(加上换行符)复制到保留空间('h')中。搜索的第二个模式是版本字符串。同样,我们仅隔离版本号 - 用空格作为前缀。然后,我们将版本号连接到保留空间('H'),并交换保留空间和模式空间('x')。我们删除解释器名称和版本号之间的换行符;然后在其前面加上 'interpreter-version: "' 前缀,并在其后面加上结束双引号并打印结果:

interpreter-version: "ruby 1.8.6"

如果您真正想要的是 'ruby-1.8.6',则更改为次要:

sed -n '/^interpreter: *"\(.*\)"/{
s//\1/
h
}
/^version: *"\(.*\)"/{
s//-\1/
H
x
s/\n//p
}'

这会在版本前添加破折号而不是空格,然后只需在交换保留和模式空格后在打印之前删除换行符。

It is easy enough to do in pure 'sed' if you're aware of the 'hold space':

sed -n '/^interpreter: *"\(.*\)"/{
s//\1/
h
}
/^version: *"\(.*\)"/{
s// \1/
H
x
s/\n//
s/.*/interpreter-version: "&"/p
}'

The script overall doesn't print anything (-n) unless you command it to. The first pattern searched for is the interpreter line; we capture the name of the interpreter, then remove all the rest of the material, and copy the interpretr name (plus a newline) into the hold space ('h'). The second pattern searched for is the version string. We similarly isolate just the version number - prefixing it with a space. Then we concatenate the version number to the hold space ('H'), and swap the hold and pattern spaces ('x'). We remove the newline in between the interpreter name and the version number; then prefix it with 'interpreter-version: "' and follow it with a closing double quote and print the result:

interpreter-version: "ruby 1.8.6"

And if what you really want is 'ruby-1.8.6', then the changes are minor:

sed -n '/^interpreter: *"\(.*\)"/{
s//\1/
h
}
/^version: *"\(.*\)"/{
s//-\1/
H
x
s/\n//p
}'

This prefixes the version with a dash instead of a space, and then only has to remove the newline before printing after exchanging the hold and pattern spaces.

呆萌少年 2024-10-11 14:26:02

1)捕获命令行工具正在输出的内容并将其保存到文件中,即某些东西>>输出文件
2) 在我的 bash/ubuntu 盒子上”

grep -e "interpreter" -e "version" outfile | awk 'BEGIN {print"Interpreter-Version" }{print $2}'| sed 's/"//g'| tr '\n' ' '

3) 输出:

Interpreter-Version ruby 1.8.6

我确信它可以优化,但它对我有用:-)

HTH,

克里斯。

1) capture to file what's being output by your command line tool i.e. something >> outfile
2) on my bash/ubuntu box"

grep -e "interpreter" -e "version" outfile | awk 'BEGIN {print"Interpreter-Version" }{print $2}'| sed 's/"//g'| tr '\n' ' '

3) The output:

Interpreter-Version ruby 1.8.6

I am sure it could be optimized but it worked for me :-)

HTH,

Chris.

感情旳空白 2024-10-11 14:26:02

这应该可以解决问题:

pik info | awk '/^interpreter:/ {inter=$2; gsub(/"/, "", inter)} /^version:/ {vers=$2; gsub(/"/, "", vers)} END {print inter "-" vers}'

生成...

ruby-1.8.6

This should do the trick:

pik info | awk '/^interpreter:/ {inter=$2; gsub(/"/, "", inter)} /^version:/ {vers=$2; gsub(/"/, "", vers)} END {print inter "-" vers}'

generates ...

ruby-1.8.6
辞别 2024-10-11 14:26:02

同时使用 awksed 的版本。

pik info | awk '/^interpreter:|^version:/ {printf "%s-", $2}' | sed -e 's/"//g' -e 's/-$//'

A version using both awk and sed.

pik info | awk '/^interpreter:|^version:/ {printf "%s-", $2}' | sed -e 's/"//g' -e 's/-$//'
追我者格杀勿论 2024-10-11 14:26:02

仅使用 sed

pik info | sed -n '/^full_version: "\([^(]*\)(.*/ {s//\1/; s/ /-/;p}'

Using only sed:

pik info | sed -n '/^full_version: "\([^(]*\)(.*/ {s//\1/; s/ /-/;p}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文