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

Re: quick scripting question - finding occurrence in many lines



michael wrote:
On Fri, 2006-11-03 at 13:32 -0800, Aleksei Dzhulai wrote:
The simplest case:
awk '{for (i=1;i<=NF;i++) {if ($i~/Processor/) print $(i-1)} }' file


I don't think that will pick up the case where the required output is on
the line before Processor, but thanks, M



I didn't catch the original post, but based on your comment, it sounds like you want to do record based processing, where the content of the prior line is processed in some one when some condition on the current line is met?

If so, this may help you out. I'm leaving out the awk invocation itself, you can put this in a file and use the '-f filename' option or put it on the command line between single quotes:


{
  if(NR == 1)
  {
    prev = $0
  }
  else
  {
    if($0 ~ /Processor/)
    {
      print prev
    }
    prev = $0
  }
}

Bob

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Reply to: