printing field from previous line

This is a discussion on printing field from previous line within the awk forums in Programming Languages category; I'm using awk to process some PHP/xdebug output, here is a sample: 69.5618 689496 -> MyNetworkImage- >connectRouterToWirelessPanel() /rw_fs/share/reserved2/var/www/hnm/ classes/MyN1 69.5650 689496 -> debugPrint() /rw_fs/share/reserved2/ var/www/hnm/classes/MyNetworkImage.class.php:273 69.5693 689496 -> imagecolorallocate() /rw_fs/share/ reserved2/var/www/hnm/classes/MyNetworkImage.class.php:276 69.5735 689496 -> imagedashedline() /rw_fs/share/ reserved2/var/www/hnm/classes/MyNetworkImage.class.php:277 69.5775 689496 -> debugPrint() /rw_fs/share/reserved2/ var/www/hnm/classes/MyNetworkImage.class.php:278 69.5807 689496 -> debugPrint() /rw_fs/share/reserved2/var/ www/hnm/classes/MyNetworkImage.class.php:163 69.5840 689496 -> xdebug_stop_trace() /rw_fs/share/ reserved2/var/www/hnm/unit_tests/UT_MyNetworkImage.php:29 69.5870 689496 I've been able to use the following to print the difference between the current line's first field and the previous line's first field, as well as the function name which is the fourth field: awk '{if (prev != "" && prev != "TRACE") ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-07-2008, 01:14 PM
jemptymethod
Guest
 
Default printing field from previous line

I'm using awk to process some PHP/xdebug output, here is a sample:

69.5618 689496 -> MyNetworkImage-
>connectRouterToWirelessPanel() /rw_fs/share/reserved2/var/www/hnm/

classes/MyN1
69.5650 689496 -> debugPrint() /rw_fs/share/reserved2/
var/www/hnm/classes/MyNetworkImage.class.php:273
69.5693 689496 -> imagecolorallocate() /rw_fs/share/
reserved2/var/www/hnm/classes/MyNetworkImage.class.php:276
69.5735 689496 -> imagedashedline() /rw_fs/share/
reserved2/var/www/hnm/classes/MyNetworkImage.class.php:277
69.5775 689496 -> debugPrint() /rw_fs/share/reserved2/
var/www/hnm/classes/MyNetworkImage.class.php:278
69.5807 689496 -> debugPrint() /rw_fs/share/reserved2/var/
www/hnm/classes/MyNetworkImage.class.php:163
69.5840 689496 -> xdebug_stop_trace() /rw_fs/share/
reserved2/var/www/hnm/unit_tests/UT_MyNetworkImage.php:29
69.5870 689496

I've been able to use the following to print the difference between
the current line's first field and the previous line's first field, as
well as the function name which is the fourth field:

awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$4; prev=
$1}' xdebug.xt | sort | tail

Below is the output, but I realize, I don't want the current line's
function name, but rather the previous line's:

0.4263 imagecolortransparent()
0.4461 imagecolortransparent()
0.4709 imagecolortransparent()
0.4902 imagedestroy()
0.4965 imagedestroy()
0.5368 debugPrint()
0.6418 imagedestroy()
17.6024 debugPrint()
17.6616 debugPrint()
25.8278 debugPrint()

So, since I was able to save the previous line's first field using
prev=$1 I figured I'd be able to do something similar to save the
previous line's fourth field, such as:

awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
$1; fname=$4}' xdebug.xt | sort | tail

However see the output I am getting pasted below; it's as though it's
printing the entire previous line instead of just the 4th field saved
into the fname variable. What am I doing wrong?

0.4263 37.1507 636840 -> imagecolortransparent() /
rw_fs/share/reserved2/var/www/hnm/classes/Panel.class.ph4
0.4461 18.9031 635880 -> imagecolortransparent() /
rw_fs/share/reserved2/var/www/hnm/classes/Panel.class.ph4
0.4709 0.7000 634480 -> imagecolortransparent() /
rw_fs/share/reserved2/var/www/hnm/classes/Panel.class.ph4
0.4902 67.7699 686392 -> imagedestroy() /rw_fs/
share/reserved2/var/www/hnm/classes/Panel.class.php:223
0.4965 66.8246 682544 -> imagedestroy() /rw_fs/
share/reserved2/var/www/hnm/classes/Panel.class.php:223
0.5368 64.1675 638568 -> debugPrint() /rw_fs/share/
reserved2/var/www/hnm/classes/MyNetworkImage.class.php:93
0.6418 68.9820 689272 -> imagedestroy() /rw_fs/share/
reserved2/var/www/hnm/classes/RouterPanel.class.php:10
17.6024 18.4349 635264 -> debugPrint() /rw_fs/share/
reserved2/var/www/hnm/classes/Panel.class.php:72
17.6616 36.7002 636264 -> debugPrint() /rw_fs/share/
reserved2/var/www/hnm/classes/Panel.class.php:72
25.8278 63.2345 637256 -> debugPrint() /rw_fs/share/
reserved2/var/www/hnm/classes/Panel.class.php:72

Thanks in advance, and I must say, I'm becoming a bit of an awk
evangelist, developing on an embedded device where there is no Perl
installed
Reply With Quote
  #2  
Old 08-07-2008, 01:19 PM
Kenny McCormack
Guest
 
Default Re: printing field from previous line

In article <82c7ec66-2a3e-4c29-b354-63efc47c4522@f36g2000hsa.googlegroups.com>,
jemptymethod <jemptymethod@gmail.com> wrote:
....
>awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
>$1; fname=$4}' xdebug.xt | sort | tail


Change $fname to fname.

Exercise for the reader: Explain what happens with it as is.
Reply With Quote
  #3  
Old 08-07-2008, 01:36 PM
jemptymethod
Guest
 
Default Re: printing field from previous line

Thanks for being my other pair of eyeballs!

On Aug 7, 1:19 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
> In article <82c7ec66-2a3e-4c29-b354-63efc47c4...@f36g2000hsa.googlegroups.com>,jemptym ethod <jemptymet...@gmail.com> wrote:
>
> ...
>
> >awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
> >$1; fname=$4}' xdebug.xt | sort | tail

>
> Change $fname to fname.
>
> Exercise for the reader: Explain what happens with it as is.


Reply With Quote
  #4  
Old 08-07-2008, 01:39 PM
Grant
Guest
 
Default Re: printing field from previous line

On Thu, 7 Aug 2008 10:14:26 -0700 (PDT), jemptymethod <jemptymethod@gmail.com> wrote:

>I'm using awk to process some PHP/xdebug output, here is a sample:
>
> 69.5618 689496 -> MyNetworkImage-
>>connectRouterToWirelessPanel() /rw_fs/share/reserved2/var/www/hnm/

>classes/MyN1
> 69.5650 689496 -> debugPrint() /rw_fs/share/reserved2/
>var/www/hnm/classes/MyNetworkImage.class.php:273
> 69.5693 689496 -> imagecolorallocate() /rw_fs/share/
>reserved2/var/www/hnm/classes/MyNetworkImage.class.php:276
> 69.5735 689496 -> imagedashedline() /rw_fs/share/
>reserved2/var/www/hnm/classes/MyNetworkImage.class.php:277
> 69.5775 689496 -> debugPrint() /rw_fs/share/reserved2/
>var/www/hnm/classes/MyNetworkImage.class.php:278
> 69.5807 689496 -> debugPrint() /rw_fs/share/reserved2/var/
>www/hnm/classes/MyNetworkImage.class.php:163
> 69.5840 689496 -> xdebug_stop_trace() /rw_fs/share/
>reserved2/var/www/hnm/unit_tests/UT_MyNetworkImage.php:29
> 69.5870 689496
>
>I've been able to use the following to print the difference between
>the current line's first field and the previous line's first field, as
>well as the function name which is the fourth field:
>
>awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$4; prev=
>$1}' xdebug.xt | sort | tail
>
>Below is the output, but I realize, I don't want the current line's
>function name, but rather the previous line's:


Like this?
~$ cat zzz
69.5618 689496 -> MyNetworkImage->connectRouterToWirelessPanel() /rw_fs/share/reserved2/var/www/hnm/classes/MyN1
69.5650 689496 -> debugPrint() /rw_fs/share/reserved2/var/www/hnm/classes/MyNetworkImage.class.php:273
69.5693 689496 -> imagecolorallocate() /rw_fs/share/reserved2/var/www/hnm/classes/MyNetworkImage.class.php:276
69.5735 689496 -> imagedashedline() /rw_fs/share/reserved2/var/www/hnm/classes/MyNetworkImage.class.php:277
69.5775 689496 -> debugPrint() /rw_fs/share/reserved2/var/www/hnm/classes/MyNetworkImage.class.php:278
69.5807 689496 -> debugPrint() /rw_fs/share/reserved2/var/www/hnm/classes/MyNetworkImage.class.php:163
69.5840 689496 -> xdebug_stop_trace() /rw_fs/share/reserved2/var/www/hnm/unit_tests/UT_MyNetworkImage.php:29
69.5870 689496

~$ awk 'prev && prev != "TRACE" {print $1-prev "\t" lfunc};{prev=$1; lfunc=$4}' zzz
0.0032 MyNetworkImage->connectRouterToWirelessPanel()
0.0043 debugPrint()
0.0042 imagecolorallocate()
0.004 imagedashedline()
0.0032 debugPrint()
0.0033 debugPrint()
0.003 xdebug_stop_trace()

Grant.
--
http://bugsplatter.id.au/
Reply With Quote
  #5  
Old 08-07-2008, 03:16 PM
Hermann Peifer
Guest
 
Default Re: printing field from previous line

Kenny McCormack wrote:
> In article <82c7ec66-2a3e-4c29-b354-63efc47c4522@f36g2000hsa.googlegroups.com>,
> jemptymethod <jemptymethod@gmail.com> wrote:
> ...
>> awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
>> $1; fname=$4}' xdebug.xt | sort | tail

>
> Change $fname to fname.
>
> Exercise for the reader: Explain what happens with it as is.


The value of variable fname is a string. In numeric operations, string values evaluate to a numeric zero, hence $fname evaluates to $0. As the OP wrote:

> it's as though it's printing the entire
> previous line instead of just the 4th field


Hermann
Reply With Quote
  #6  
Old 08-07-2008, 06:00 PM
jemptymethod
Guest
 
Default Re: printing field from previous line

On Aug 7, 3:16 pm, Hermann Peifer <pei...@gmx.net> wrote:
> Kenny McCormack wrote:
> > In article <82c7ec66-2a3e-4c29-b354-63efc47c4...@f36g2000hsa.googlegroups.com>,
> > jemptymethod <jemptymet...@gmail.com> wrote:
> > ...
> >> awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
> >> $1; fname=$4}' xdebug.xt | sort | tail

>
> > Change $fname to fname.

>
> > Exercise for the reader: Explain what happens with it as is.

>
> The value of variable fname is a string. In numeric operations, string values evaluate to a numeric zero, hence $fname evaluates to $0.


What's the "numeric operation" that was causing the string value of
fname to be evaluated to zero? I guess I'm not understanding the
precise nature of the $ operator, if that is what it indeed is (an
operator that is).

Thanks, I'm getting a reality check from you guys, I thought I was
getting fairly advanced with Awk, but I see I still have a *long way*
to go
Reply With Quote
  #7  
Old 08-07-2008, 06:12 PM
Janis Papanagnou
Guest
 
Default Re: printing field from previous line

jemptymethod wrote:
> On Aug 7, 3:16 pm, Hermann Peifer <pei...@gmx.net> wrote:
>
>>Kenny McCormack wrote:
>>
>>>In article <82c7ec66-2a3e-4c29-b354-63efc47c4...@f36g2000hsa.googlegroups.com>,
>>>jemptymethod <jemptymet...@gmail.com> wrote:
>>>...
>>>
>>>>awk '{if (prev != "" && prev != "TRACE") print $1-prev"\t"$fname; prev=
>>>>$1; fname=$4}' xdebug.xt | sort | tail

>>
>>>Change $fname to fname.

>>
>>>Exercise for the reader: Explain what happens with it as is.

>>
>>The value of variable fname is a string. In numeric operations, string values evaluate to a numeric zero, hence $fname evaluates to $0.

>
>
> What's the "numeric operation" that was causing the string value of
> fname to be evaluated to zero? I guess I'm not understanding the
> precise nature of the $ operator, if that is what it indeed is (an
> operator that is).


Maybe it's clearer if you compare the following expressions...

print $4
print $(2+2)
x=3; print $(x+1)
fname=3; print $ (fname+1)
fname=4; print $fname
fname="some string"; print $fname

In the latter case "some string" is interpreted as number in
the context of the field access operator $, so the string will
be evaluated to the value 0, and $0 is accessed.

Janis

>
> Thanks, I'm getting a reality check from you guys, I thought I was
> getting fairly advanced with Awk, but I see I still have a *long way*
> to go

Reply With Quote
  #8  
Old 08-07-2008, 07:32 PM
jemptymethod
Guest
 
Default Re: printing field from previous line

On Aug 7, 6:12 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
>
> Maybe it's clearer if you compare the following expressions...
>
> print $4
> print $(2+2)
> x=3; print $(x+1)
> fname=3; print $ (fname+1)
> fname=4; print $fname
> fname="some string"; print $fname
>
> In the latter case "some string" is interpreted as number in
> the context of the field access operator $, so the string will
> be evaluated to the value 0, and $0 is accessed.


Thanks very much. So the field access operator of necessity requires
an integer, presumably non-negative, and as indicated previously,
which I've since confirmed in the online awk manual (http://
http://www.gnu.org/manual/gawk/html_...onversion.html), "strings that
can't be interpreted as valid numbers convert to zero."
Reply With Quote
  #9  
Old 08-08-2008, 04:34 AM
Hermann Peifer
Guest
 
Default Re: printing field from previous line

jemptymethod wrote:
> On Aug 7, 6:12 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>> Maybe it's clearer if you compare the following expressions...
>>
>> print $4
>> print $(2+2)
>> x=3; print $(x+1)
>> fname=3; print $ (fname+1)
>> fname=4; print $fname
>> fname="some string"; print $fname
>>
>> In the latter case "some string" is interpreted as number in
>> the context of the field access operator $, so the string will
>> be evaluated to the value 0, and $0 is accessed.

>
> Thanks very much. So the field access operator of necessity requires
> an integer, presumably non-negative, and as indicated previously,
> which I've since confirmed in the online awk manual (http://
> http://www.gnu.org/manual/gawk/html_...onversion.html), "strings that
> can't be interpreted as valid numbers convert to zero."



> requires an integer


....or a string, including the empty string. (It could well be that one intentionally wants to access $0, in case an expression or variable evaluates to zero.)


> presumably non-negative...


Indeed. Trying to access $(-1) results in this error message: fatal: attempt to access field -1

While thingking twice about the issue, I also guess that your observation above:

> it's as though it's printing the entire previous line instead of just the 4th field


....should perhaps read:

> it's as though it's printing the entire *current* line instead of just the 4th field of the *previous line*


Hermann

PS: Thanks to Janis for his more comprehensive explanation about the field access operator.
Reply With Quote
  #10  
Old 08-08-2008, 05:02 AM
Hermann Peifer
Guest
 
Default Re: printing field from previous line

Just a small addition as I saw that eval.c has some helpful lint warnings:

gawk --lint '{foo=""; bar="hello world"; print $foo,$bar}'
abc
gawk: (FILENAME=- FNR=1) warning: attempt to field reference from non-numeric value
gawk: (FILENAME=- FNR=1) warning: attempt to reference from null string
gawk: (FILENAME=- FNR=1) warning: attempt to field reference from non-numeric value
abc abc


Hermann
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:36 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.