Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing first occurence of a input line

Status
Not open for further replies.

REGYS

Technical User
Apr 18, 2005
1
US
Hi guys,
I'm trying to print the first occurence of an input line in a log file using an if statement and an index function. Check it out.

if (index($0,"MS Position Req(msgLen=23)")>0 ){
my_string = $0;
split(my_string,time,":");
substr($1,2,7);
split($1,t,":");
$1=(t[2]*60)+ t[3];
print "MS Position:",$1;
As a result all the input lines with "MS Position Req" get printed instead of the first occurence just like i wish.
Can someone help me
 
Put an exit statement after the print and the program will exit after the first execution of the if block.

Or set a flag in a BEGIN block and change it in the if block, e.g.
Code:
[b]BEGIN {first = 1}[/b]
if ([b]first[/b] && index($0,"MS Position Req(msgLen=23)")>0 ){
    my_string = $0;
    split(my_string,time,":");
    substr($1,2,7);
    split($1,t,":");
    $1=(t[2]*60)+ t[3];
    print "MS Position:",$1;
    [b]first = 0[/b]
}

 
You need braces around that if block, of course...
Code:
[b]BEGIN {first = 1}[/b]
[b]{[/b]
    if ([b]first[/b] && index($0,"MS Position Req(msgLen=23)")>0 ){
        my_string = $0;
        split(my_string,time,":");
        substr($1,2,7);
        split($1,t,":");
        $1=(t[2]*60)+ t[3];
        print "MS Position:",$1;
        [b]first = 0[/b]
    }
[b]}[/b]



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top