Skip to content
Advertisement

How do I grep/awk multiple lines from a cluster based on a pattern?

is there a way I can grep/awk multiple lines from a cluster based on a pattern?

I have a file as follows:

File.txt

>Cluster1
1 rabbit eats carrot 
2 Lion is the king of jungle
3 Dogs loves toys 
4 Cats loves mice 
>Cluster2
1 Horse loves grass 
2 Giraffes love leaves
3 Hippos love water
>Cluster3
1 Snakes love trees 
2 Sharks love fish
3 Tigers love bushes 
4 Cats love toys
5 Dogs love food
>Cluster4
1 Leopards love running 
2 Dogs love toys
3 Cats love food
>Cluster5
1 rabbit eats carrot 
2 Leopards love running
3 Cats love food

And the pattern is – “Dogs”, I would like the output to be:

>Cluster1
1 rabbit eats carrot 
2 Lion is the king of jungle
3 Dogs loves toys 
4 Cats loves mice
>Cluster3
1 Snakes love trees 
2 Sharks love fish
3 Tigers love bushes 
4 Cats love toys
5 Dogs love food
>Cluster4
1 Leopards love running 
2 Dogs love toys
3 Cats love food

Is this possible?

Advertisement

Answer

perl -0777ne'print grep /bDogsb/, split /^(?=>)/m, $_' file

or

perl -ne'
   sub p { print $buf if $buf =~ /bDogsb/; }
   if (/^>/) { p(); $buf = ""; }
   $buf .= $_;
   END { p() }
' file

Notes:

  • The first version loads the entire file into memory (but not the second).
  • Both versions search the first line of the record as well as the subsequent lines.
  • You can place the second program all on one line if you want.
  • These address two bugs in karakfa’s answer:
    • These don’t add a leading blank line if the first record doesn’t match.
    • These don’t remove the final line feed if the last record doesn’t match.
  • Related: Specifying file to process to Perl one-liner.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement