首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在bash (多行)中获取两个单词之间的文本

在bash (多行)中获取两个单词之间的文本
EN

Stack Overflow用户
提问于 2016-07-08 22:35:44
回答 2查看 178关注 0票数 0

我正在努力解决这个问题。我有一条短信:

代码语言:javascript
复制
Executables:   manatee-curl
Dependencies:  base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
               stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
               text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
               old-locale -any, glib >=0.12.0, gio >=0.12.0,
               filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
               network -any, curl >=1.3.7, directory -any,
               template-haskell -any, derive -any, binary -any,
               regex-tdfa -any, dbus-core -any
Cached:        No

我想把所有这些词放在“属地:”和“缓存”之间。可以将此文本视为变量,例如:

echo $text \ grep /dostuff/

要明确的是,我想得到的输出是:

代码语言:javascript
复制
base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
old-locale -any, glib >=0.12.0, gio >=0.12.0,
filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
network -any, curl >=1.3.7, directory -any,
template-haskell -any, derive -any, binary -any,
regex-tdfa -any, dbus-core -any

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2016-07-08 22:44:48

您可以使用sed在两个模式之间获取文本:

代码语言:javascript
复制
text=$(sed -n '/^Dependencies:/,/^Cached:/{$d;s/Dependencies: *//;p;}' file)

echo "$text"

base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
               stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
               text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
               old-locale -any, glib >=0.12.0, gio >=0.12.0,
               filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
               network -any, curl >=1.3.7, directory -any,
               template-haskell -any, derive -any, binary -any,
               regex-tdfa -any, dbus-core -any

IDEOne工作演示

票数 0
EN

Stack Overflow用户

发布于 2016-07-09 04:44:24

awk去营救!

代码语言:javascript
复制
$ awk '/^Dependencies:/{$1="";p=1} /^Cached:/{p=0} p{sub(/^ +/,"");print}' file

base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
old-locale -any, glib >=0.12.0, gio >=0.12.0,
filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
network -any, curl >=1.3.7, directory -any,
template-haskell -any, derive -any, binary -any,
regex-tdfa -any, dbus-core -any

您可以像往常一样为一个变量赋值。

代码语言:javascript
复制
$ text=$(awk ...)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38276304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档