It is currently Tue May 22, 2018 5:45 am

Awk pattern matching name among records

All times are UTC - 6 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Dec 01, 2013 9:50 pm   

Joined: Sun Dec 01, 2013 9:47 pm
Posts: 2

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)[s][/s]. Any help would be appreciated. Thank you! [-o<

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

 PostPosted: Mon Dec 02, 2013 12:45 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 643

it's not an easy task for a beginner, though it's not quite complicated. :)

see Gawk user's guide #Multiple-Line record

 PostPosted: Mon Dec 02, 2013 9:09 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Is it homework?

Here comes a peaceful creature that'll do it for you. : -)
#!/usr/bin/awk -f
# example: ./script 'ingrid bergman' file

           ARGV[1]=""}NR==1  ||  /^[[:space:]]*$/{NR\
          ==1?film=$0:getline film;getline;date=$NF
   name=temp[1];sub(  /^[[:space:]]*/,  "",name);delete temp
   if(name!=actor) next;real_name=name;append=(date in films)
  if(_films[n--]  ==film)exists   =1;delete  _films;if(exists)
  next;films     [date]=append?films[date]    SUBSEP film:film}
        END{if(!   real_name)exit(1);print  real_name
          for(date           in            films){
             print  _films[n--];delete _films
                     #  #     #
            # # #   #        #
           #         #     #       #  #
             #          #  #        #
                #  #  #      #  #
                         # # #

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group