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

Get text between two words in bash (multilines)

我正在为这个问题而苦恼。我有这段文字:

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

我想获取 "Dependencies:" 和 "Cached" 之间的所有单词。 您可以将此文本视为变量,例如:

回声$文本 | grep /dostuff/

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

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

谢谢。

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

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 Working Demo

awk 救援!

$ awk '/^Dependencies:/{="";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

你可以像往常一样给变量赋值

$ text=$(awk ...)

使用 GNU grep:

echo "$text" | grep -Poz 'Dependencies:  \K(.*\n)*(?=Cached:)' | grep -Po '^ +\K.*'

输出:

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

参见:The Stack Overflow Regular Expressions FAQ