例如下面的文字,在 Linux 中,如何将下面的部分从“将安装”移到“建议的软件包”之前的文字。
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.
获取的输出只是“ipython3-notebook ipython3-qtconsole python3-zmq”,没有开始和结束模式。
答案1
使用 GNUsed
而不是grep
:
$ gsed -n '/The following extra packages will be installed:/{:a;n;/Suggested packages:/q;p;ba}' text
当行包含文本时The following extra packages will be installed
,sed
编辑脚本将创建一个名为的标签a
(以便稍后跳转),然后读取下一行(n
)。如果该行包含文本,Suggested packages:
脚本将终止(q
),否则它将打印该行(p
)并分支回先前创建的标签(ba
),该标签读取下一行,依此类推。