在bash中打印两个标记之间的文本

时间:2014-09-25 15:05:43

标签: bash

我有以下文件:

$ cat animals

All birds
------------------------------------------------------------------------------
Birds in database                 size     date
------------------------------------------------------------------------------
Pink flamingos                     77       20130830
Black crows                       997       20070122

All mammals
------------------------------------------------------------------------------
Mammals in database               size     date 
------------------------------------------------------------------------------
brown cats                        34        20111201
purple hippos                     23        20120220
white dogs                         9        20130926
black horses                       2        20050428
pink monkeys                     118        19940203

All reptiles
------------------------------------------------------------------------------
Reptiles in database              size     date 
------------------------------------------------------------------------------
red snakes                        44       20070125
green frogs                     1022       20140707

$ 

我只想打印Mammals in database部分的内容,即输出需要:

brown cats                        34        20111201
purple hippos                     23        20120220
white dogs                         9        20130926
black horses                       2        20050428
pink monkeys                     118        19940203

我可以使用sed

轻松完成此操作
$ gsed -n '/Mammals in database/,/^$/ { /Mammals in database/N; /Mammals in database/d; /^$/d; p }' < animals
brown cats                        34        20111201
purple hippos                     23        20120220
white dogs                         9        20130926
black horses                       2        20050428
pink monkeys                     118        19940203
$ 

然而,是否有一种优雅的方式来实现与bash相同的效果?

1 个答案:

答案 0 :(得分:3)

我喜欢使用awk

$ awk '!NF{f=0} NF>3 && f; /Mammals in database/ {f=1}' file
brown cats                        34        20111201
purple hippos                     23        20120220
white dogs                         9        20130926
black horses                       2        20050428
pink monkeys                     118        19940203

这是一个标志f的问题,并根据条件处理它开启或关闭。


纯粹的bash草案方法:

var=""
while read -r line
do
   [[ $line =~ ^"All reptiles"* ]] && var=""
   [ "$var" ] && [ "$line" ] && echo "$line"
   [[ $line =~ ^"Mammals in database"* ]] && var=1
done < file

测试

$ while read -r line ; do [[ $line =~ ^"All reptiles"* ]] && var=""; [ "$var" ] && [ "$line" ] && echo "$line"; [[ $line =~ ^"Mammals in database"* ]] && var=1; done < file
------------------------------------------------------------------------------
brown cats                        34        20111201
purple hippos                     23        20120220
white dogs                         9        20130926
black horses                       2        20050428
pink monkeys                     118        19940203