[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: quick scripting question - finding occurrence in many lines



On Fri, Nov 03, 2006 at 08:27:42PM +0000, michael wrote:
> I've been trying to do this with 'awk' but am hitting probs (not used
> awk for ages!) so all offers welcome! 
> 
> Given a multiple line file, IN, that contains the word Processor
> (possibly split over 2 lines) I wish to output the field immediately
> preceeding Processor.
> 
> eg for
> 
> junk info 18 Pro
> cessor
> 
> I wish to get the field '18'

Used to use awk a lot, now mostly use perl, but here's something in awk:

    #!/usr/bin/awk -f

    part1 { # part of search term is found
      # print "TESTING", part1, $1
        part12 = part1 $1
        if ( part12 == "Processor" )
            print saved
        part1 = ""
        }
    
    ! part1 && $NF ~ /^P/ {
      # print "TRYING", $NF
        if ( $NF == "Processor" )
            print $(NF-1)
        else
            part1 = $NF
            saved = $(NF-1)
          # print "partway there:", save, part1
        }

-- 
Ken Irving, fnkci@uaf.edu



Reply to: