Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 02-23-2012, 06:16 AM   #1
K_märkt
Prospect
 
Join Date: Sep 2009
Posts: 10
awk: how call field values from different lines

Hi,

I have a sample file that looks as follows:

ext 25 /
ext 36 |
ext 43 /
ext 87 |
ext 95 /
ext 234 /
ext 345 |
ext 500 /
ext 536 |
.
.

My desired output is to print on one line: field 1, field 2 from first line, field 2 from the second line, and the difference between lines of field 2 (see below). Note that when / is not followed by | I need to signal this with a zero length in the fourth field of the output.

type start stop length
ext 25 36 11
ext 43 87 44
ext 95 95 0
ext 234 345 111
ext 500 536 36

I've been fumbling with awk but my problem is to call the field values from different line records. Thanks for any suggestions.

Best,
K
K_märkt is offline   Reply With Quote
Old 02-23-2012, 09:58 AM   #2
brettgrant99
All Star
 
Join Date: May 2007
Posts: 674
Something like this?

Code:
#! /bin/awk -f
BEGIN{
   count=1
   print "type start stop length"
}
{
  if($NF ~ /\// ) {
     name[count] = $1
     val1[count] = $2
     count++
     flg = 1
  }
  if($NF ~ /\|/ && flg = 1) {
     val2[count-1] = $2
  }
}
END{
   for(i=1;i<count;i++)
      if(length(val2[i]) == 0)
         print name[i],val1[i],val1[i],0
      else
         print name[i],val1[i],val2[i],val2[i]-val1[i]
}
HTH,
Brett
brettgrant99 is offline   Reply With Quote
Old 02-23-2012, 11:32 AM   #3
K_märkt
Prospect
 
Join Date: Sep 2009
Posts: 10
That really did help - thanks a lot!

What I don't understand is the assignment of flg = 1 in the first if statement or the logical AND flg = 1 in the second if statement? What does this do?

Maybe there is no time to answer stuff like that, but thanks again for the help!

Best,
K
K_märkt is offline   Reply With Quote
Old 02-23-2012, 12:19 PM   #4
fracai
MVP
 
Join Date: May 2004
Posts: 2,084
I think 'flg' is ensuring that a "first" line has been seen before processing a "second" line.

It's protecting against something like:
ext 25 /
ext 36 |
ext 46 |
ext 43 /
ext 87 |

Though, I'd think there'd need to be something like:
Code:
  if($NF ~ /\|/ && flg = 1) {
     val2[count-1] = $2
     flg = 0
  }
So, maybe I'm incorrect in my analysis.
fracai is offline   Reply With Quote
Old 02-23-2012, 12:34 PM   #5
brettgrant99
All Star
 
Join Date: May 2007
Posts: 674
You actually don't need "flg"

Originally I was trying to keep a status on the lines, but it turned out to be easier to do it the way it is.

Of course it isn't very flexible. It only checks for two / characters, not two | characters. But it does give one an idea on how to check multiple lines.

Also, as all the data is being stored in arrays, there is probably a size limit as it doesn't really operate on a Line by Line processing, at least the output doesn't.

Brett
brettgrant99 is offline   Reply With Quote
Old 02-25-2012, 06:56 AM   #6
K_märkt
Prospect
 
Join Date: Sep 2009
Posts: 10
Thank you for your answers.

/K
K_märkt is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:30 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.