Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk pattern matching movie edition

Status
Not open for further replies.

JillCeke

Technical User
Dec 1, 2013
5
0
0
US
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 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!

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






 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tried to extract the actors' names and use them as keys using a file i created:

NF==3 {print $2 $1, $0}
NF==4 {print $3 $1 $2, $0}

Now I tried to sort them but I totally lost on the looping structure. Here is my second
file I created to code the loop

NF==3 {print $2 $1, $0}
NF==4 {print $3 $1 $2, $0}

when using the sort function and piping in these two programs and the database I get this:

Felicity Jones: Anna 0
United Artists 1980
Liam Hemsworth: Gale Hawthorne
Ingrid Bergman: Ilsa Lund
Anton Yelchin: Jacob Helm
Joe Pesci: Joey LaMotta
Jennifer Lawrence: Katniss Everdeen
Jennifer Lawrence: Samantha
Josh Hutcherson: Peeta Mellark
Humphrey Bogart: Rick Blaine
Cathy Moriarty: Vickie Thailer
Paul Henreid: Victor Laszlo

I don't know what to do from this point out or if I'm going in the right direction. Also I don't know how to account for the spaces between records and that semi-colon between actor and character role.

 
Sorry typo. My second file code is actually this:

NR==1 || $1==prevkey {total = total + $NF}
NR>1 && $1!=prevkey {print prevname, total; total = $NF}
{prevkey = $1; $1 = ""; $NF = ""; prevname = $0}
END {print prevname, total}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top