awk pattern matching name in records

Hi,

I'm very new to these forums. I was wondering if someone could help an AWK beginner with a pattern matching
an actor to his appearance in movies, which would be stored as records. Let's say we have a database of 4 movies (each movie a record with name, studio + year, and actor fields with a blank line separating the four records) and need to pattern match and actor's name and return his name followed by the movies he has appeared in (in chronological order). In this case I would like to pattern match Jennifer Lawrence and create an output file that lists Jennifer Lawrence at the top followed by the names of movies she has appeared in by chronological order, each on separate lines (just an output file of 3 lines). Any help would be appreciated. Thank you! :slight_smile:

Casablanca
WB 1942
Humphrey Bogart: Rick Blaine
Ingrid Bergman: Ilsa Lund
Paul Henreid: Victor Laszlo

Hunger Games
Lionsgate 2012
Jennifer Lawrence: Katniss Everdeen
Josh Hutcherson: Peeta Mellark
Liam Hemsworth: Gale Hawthorne

Like Crazy
Paramount 2011
Anton Yelchin: Jacob Helm
Felicity Jones: Anna Gardner
Jennifer Lawrence: Samantha

Raging Bull
United Artists 1980
Robert de Niro: Jake LaMotta
Joe Pesci: Joey LaMotta
Cathy Moriarty: Vickie Thailer

This could be done with awk, try something like this:

awk '/Jennifer Lawrence/{print $1}' FS='\n' RS= file

This sets the records separator to an empty line (two consecutive linefeeds ( RS= )and the field separator to a single line feed ( FS='\n')

To also list the actor's name, for example:

awk -v s="Jennifer Lawrence" 'BEGIN{print s} $0~s{print $1}' FS='\n' RS= file

Based on Scrutinizer's proposal, this might come closer to OP's request (chronological order):

awk -v NM="Jennifer Lawrence" 'BEGIN {print NM} $0~NM {print $1, $2} ' RS= FS="\n"  file | sort  -k4,4 
Jennifer Lawrence
Like Crazy Paramount 2011
Hunger Games Lionsgate 2012

I know it would fail miserable on Casablanca.