INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Awk pattern matching movie edition

Awk pattern matching movie edition

(OP)
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






RE: Awk pattern matching movie edition

(OP)
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.

RE: Awk pattern matching movie edition

(OP)
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}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close