awk: last occurance

Hi All,

I need to extract the last occurance of a pattern match. So far I've got the code below which extracts the first occurance. Any ideas how I can modify it so that it extracts the last?

BEGIN {}                        
{                               
   if (data[$2]++ == 0)         
      lines[++count] = $0       
}                               
END {                           
    for (i = 1; i <= count; i++)
        print lines          
}                               

Thanks in advance,

p.

could you paste an example what exactly you are looking for?

how abt if you serach you pattern first and then use awk to print last line/last field?
Or
simply add your search algorithm in the statment section of awk instead of having it in BEGIN section. Not sure about your requirement but above are the possibilities as far as I could understand the question.

--Manish

{  
   lines[$2] = $0                                 
}                               
END {                           
    for (i in lines)
        print lines          
}                   

Don't care about what i've said ... i've misred the original post

hmmmmmm..... intersting - did you say anything on this thread at all?

yes, but i've edited it

I must be going blind here...

vgersh99, the remaining post is confusing, but I think what happened is that the post that says "to ignore" is the only one posted, it was edit within the timelimit for not showing the edit information at the bottom.

ah I see - I thought ALL of the edits would be reflected in the posts.
Well.... I've been wrong before.

Thanks for the clarification, reborg!

Reborg,

Whats the timelimit for the edit by the way? I think 30 minutes?

Here's my sample file called test

To print all values matching pattern:

awk '$0~"test1"' test

or

 awk '$0~"test1"{var=$0;print var}' test 

To print just the last occurence

 awk '$0~"test1"{var=$0}END{print var}' test 

Hi All,

vgersh99 this was the one I was after, thanks!

{                              
   lines[$2] = $0              
}                              
END {                          
    for (i in lines)           
        print lines         
}

Now I realise that now that's done (duplicates removed and latest only output based on $2) I need to sort this output by the next field ($3) numerically. Any ideas?

Cheers, p.

sort -t" " -kn3 file

Hi anbu23,

Thanks for this. I'm not on Solaris so I've made a minor change (at least that's why I think I had to change it...)

 /test/nigelp> awk -F, -f alg_awk_2 test.alg | sort -t, +3n      
TEST:HOME,01010515,20061230204800,,0794,10,DIAG;                    
TEST:HOME,0102050A,20061230205838,,1094,10,DIAG;                    
TEST:HOME,01020518,20061230213158,,2844,10,DIAG;                    
TEST:HOME,01020502,20061230185622,,7397,10,DIAG;                    
TEST:HOME,01010513,20061230195958,,8721,10,DIAG;                    
TEST:HOME,0102051A,20061230201042,,8998,10,DIAG;                    
TEST:HOME,01020514,20061230202733,,9620,10,DIAG;                    

Interestingly it's not sorting field 3 as I'd expect - it's a date time field which I would've thought should sort numerically. Am I doing something wrong?

Also I'd really like the sort to be contained within the awk to keep things elegant. Is there a way of doing this?

Many thanks, p.

I've just spotted my sort command error: +3n isn't field 3 +2n is actually the 3rd field reference...

I'd still like to contain the sort within the awk if possible, anyone know how I can do this?

This is the awk that I'm using:

{                       
   lines[$2] = $0       
}                       
END {                   
    for (i in lines)    
        print lines  
}

Thanks again, p.

 /test/nigelp> awk -F, -f alg_awk_2 test.alg | sort -t, +3n 

+3 means numeric key beginning at the start of fourth field

It's a setting which can only be set/viewed from the admin control panel, so I guess you'd have to ask Perderabo or Neo, however the default in Vb for 3.0.11 appears to be 2 minutes so I'm guessing it's the same here.

The time limit for actually being able to edit a post is usually forever.