0

i am fighting with this issue. I have this text:

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

and i want to get all those words between "Dependencies:" and "Cached". you can treat this text as a variable, for example:

echo $text | grep /dostuff/

To be clear, the output I want to get is:

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

Thank you.

nhed
  • 5,268
  • 2
  • 25
  • 39
Christopher Loen
  • 127
  • 1
  • 1
  • 6

3 Answers3

0

You can use sed to get text between 2 patterns:

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

anubhava
  • 664,788
  • 59
  • 469
  • 547
0

awk to the rescue!

$ 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

you can assign to a variable as usual

$ text=$(awk ...)
karakfa
  • 62,998
  • 7
  • 34
  • 47
0

With GNU grep:

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

Output:

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

See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117