For example text below , in Linux, how to get the section below from the "will be installed" to the text before "Suggested packages".

Use 'apt-get autoremove' to remove it.
The following extra packages will be installed:
   python3-decorator python3-simplegeneric
Suggested packages:
  ipython3-notebook ipython3-qtconsole python3-zmq
The following NEW packages will be installed:
  ipython3 python3-decorator python3-simple-generic
0 upgraded, 3 newly installed, 0 to remove and 13 not upgraded.

And the output to get is simply " ipython3-notebook ipython3-qtconsole python3-zmq" , WITHOUT the beginning and ending pattern.

  • grep regular expression syntax
    – DavidPostill
    Jul 7, 2016 at 16:02
  • @NathaneilCapital, have you tried anything? It's a very simple problem, and a google for grep will give you some examples.
    – jehad
    Jul 7, 2016 at 16:47
  • This question is unclear.  It should show the desired outcome/result, inasmuch as the OP's informal description of it seems to be giving rise to multiple interpretations. The way I interpret it, it is not a very simple problem, and @DavidPostill's link is not particularly useful. Jul 7, 2016 at 17:40
  • @G-Man It's a starting point for the OP to figure out what he's trying to do ...
    – DavidPostill
    Jul 7, 2016 at 17:42
  • @DavidPostill Added the intended result, appreciate if you can help Jul 8, 2016 at 21:52

1 Answer 1


Using GNU sed rather than grep:

$ gsed -n '/The following extra packages will be installed:/{:a;n;/Suggested packages:/q;p;ba}' text

When the line containing the text The following extra packages will be installed, the sed editing script will create a label called a (to jump back to later) and then read the next line (n). If that line contains the text Suggested packages: the script will terminate (q), otherwise it prints the line (p) and branches back to the label previously created (ba) which reads the next line, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.