Easy but not 
Author Message
 Easy but not

summary:
Find two different kinds of matches in input file then print only the
last one found.

Where the two possible matches have different number of fields and you
want only a certain part printed

Input file:

Wed May 19 18:03:50 PDT 1999 blab
Wed May 19 18:04:04 PDT 1999 blabbity
Wed May 19 18:04:17 PDT 1999 blob 123-1234
Wed May 19 18:04:34 PDT 1999 blob blab 1234-1234
Wed May 19 18:05:01 PDT 1999 blub
Wed May 19 18:05:16 PDT 1999 blob 123-1234

I can get the lines and fields I want (The Date and the numbers at the
end of the lines) with:

awk '/ [0-9]..-/{print $2,$3,$4,$8};/ [0-9]...-/{print $2,$3,$4,$9}' FILE

But I want to print only the last found match.  So instead of
outputting:

May 19 18:04:17 123-1234
May 19 18:04:34 1234-1234
May 19 18:05:16 123-1234

I want only:

May 19 18:05:16 123-1234

(or the other one if it is last)

--
Running Redhat Linux 5.2
http://www.*-*-*.com/ ~reader  



Sun, 04 Nov 2001 03:00:00 GMT  
 Easy but not


Quote:
>summary:
>Find two different kinds of matches in input file then print only the
>last one found.

>Where the two possible matches have different number of fields and you
>want only a certain part printed

>Input file:

>Wed May 19 18:03:50 PDT 1999 blab
>Wed May 19 18:04:04 PDT 1999 blabbity
>Wed May 19 18:04:17 PDT 1999 blob 123-1234
>Wed May 19 18:04:34 PDT 1999 blob blab 1234-1234
>Wed May 19 18:05:01 PDT 1999 blub
>Wed May 19 18:05:16 PDT 1999 blob 123-1234

>I can get the lines and fields I want (The Date and the numbers at the
>end of the lines) with:

>awk '/ [0-9]..-/{print $2,$3,$4,$8};/ [0-9]...-/{print $2,$3,$4,$9}' FILE

>But I want to print only the last found match.  So instead of
>outputting:

>May 19 18:04:17 123-1234
>May 19 18:04:34 1234-1234
>May 19 18:05:16 123-1234

>I want only:

>May 19 18:05:16 123-1234

>(or the other one if it is last)

Assign a value to a the same variable name.  It will retain the last
value assigned.  When done, print the variable. Something like this:

awk '/ [0-9]..-/  {a = $2 "" $3 "" $4 "" $8};
     / [0-9]...-/ {a = $2 "" $3 "" $4 "" $9}
     END{print a}' FILE

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 05 Nov 2001 03:00:00 GMT  
 Easy but not

Quote:



>>summary:
>>Find two different kinds of matches in input file then print only the
>>last one found.

>>Where the two possible matches have different number of fields and you
>>want only a certain part printed

>>Input file:

>>Wed May 19 18:03:50 PDT 1999 blab
>>Wed May 19 18:04:04 PDT 1999 blabbity
>>Wed May 19 18:04:17 PDT 1999 blob 123-1234
>>Wed May 19 18:04:34 PDT 1999 blob blab 1234-1234
>>Wed May 19 18:05:01 PDT 1999 blub
>>Wed May 19 18:05:16 PDT 1999 blob 123-1234

>>I can get the lines and fields I want (The Date and the numbers at the
>>end of the lines) with:

>>awk '/ [0-9]..-/{print $2,$3,$4,$8};/ [0-9]...-/{print $2,$3,$4,$9}' FILE

>>But I want to print only the last found match.  So instead of
>>outputting:

>>May 19 18:04:17 123-1234
>>May 19 18:04:34 1234-1234
>>May 19 18:05:16 123-1234

>>I want only:

>>May 19 18:05:16 123-1234

>>(or the other one if it is last)

>Assign a value to a the same variable name.  It will retain the last
>value assigned.  When done, print the variable. Something like this:

>awk '/ [0-9]..-/  {a = $2 "" $3 "" $4 "" $8};
>     / [0-9]...-/ {a = $2 "" $3 "" $4 "" $9}
>     END{print a}' FILE

OOPS, forgot to include spaces, make that:

awk '/ [0-9]..-/  {a = $2 " " $3 " " $4 " " $8};
     / [0-9]...-/ {a = $2 " " $3 " " $4 " " $9}
     END{print a}' FILE

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 05 Nov 2001 03:00:00 GMT  
 Easy but not

Quote:





> >>summary:
> >>Find two different kinds of matches in input file then print only the
> >>last one found.

> >>Where the two possible matches have different number of fields and you
> >>want only a certain part printed

<snip>

Quote:
> >>(or the other one if it is last)

> >Assign a value to a the same variable name.  It will retain the last
> >value assigned.  When done, print the variable. Something like this:

> >awk '/ [0-9]..-/  {a = $2 "" $3 "" $4 "" $8};
> >     / [0-9]...-/ {a = $2 "" $3 "" $4 "" $9}
> >     END{print a}' FILE

> OOPS, forgot to include spaces, make that:

> awk '/ [0-9]..-/  {a = $2 " " $3 " " $4 " " $8};
>      / [0-9]...-/ {a = $2 " " $3 " " $4 " " $9}
>      END{print a}' FILE

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


Of course, in this case, you could always run the output thru 'tail -1'
to get the last line....


Tue, 06 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software