Awk pattern matching a name within a record


Recommended Posts

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! :D

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
 

Link to comment
Share on other sites

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! :D

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

 

var="Jennifer Lawrence"; echo $var; awk -F".:." -v var="$var" '/'"$var"'/{print $2}' FILENAME

EDIT: Throw that in a shell script and redirect to a file.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.