Awk from right to left

Awk from right to left

Post by Barry Margoli » Tue, 19 May 1998 04:00:00




>Help!  I have many (100's) of logfiles I am trying to analyze.  Each entry in
>the logfile ends with the output from `date`
>i.e.

>Start Nightly System backup Sun May 17 22:55:03 EDT 1998
>Finish Nightly System backup Mon May 18 06:00:09 EDT 1998

>The number of fields in each line varies -- there are not always 6 fields
>before the actual hh:mm:ss record.

>Any way to use awk (or something else) to grab the 3rd field from the end of
>the line?  I tried something like awk '{(x=$NF - 3); print $x}' but couldn't
>get it.  (Please note that my syntax there is not correct, I know that.  I
>don't have the script in front of me.

You're close:

awk '{x = NF - 3; print $x;}'

NF is the number of fields, $NF is the contents of the last field.

Note that if you want the time, you should be using NF-2; (NF is the year
field, NF-1 is the zone, and NF-2 is the next field to the left).

--

GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

 
 
 

Awk from right to left

Post by Roel Sleger » Tue, 19 May 1998 04:00:00


Here's one way to do it:
--------------------------------------------------
awk '{
split($0, FIELD," ");
print FIELD[NF - 2];

Quote:}'  yourlogfile

--------------------------------------------------

(I put it on seperate lines for readability)

Explanation:
The "split" command creates from $0 (= current input line) an array named
FIELD based on the specified field seperator (" ").

Good luck

 
 
 

Awk from right to left

Post by Carlos J. G. Duar » Tue, 19 May 1998 04:00:00



> Start Nightly System backup Sun May 17 22:55:03 EDT 1998
> Finish Nightly System backup Mon May 18 06:00:09 EDT 1998
> The number of fields in each line varies -- there are not always 6 fields

thw anser with awk, as been given already. but note, that
if all you want is to grab the hh:mm:ss, you can do it
like this:

        sed 's/.*\([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1/'

on your files.

(hm.. on the other hand, it isn't simpler, and
won't be faster)

carlos

 
 
 

Awk from right to left

Post by Adam Lovic » Wed, 20 May 1998 04:00:00



Quote:>Help!  I have many (100's) of logfiles I am trying to analyze.  Each entry in
>the logfile ends with the output from `date`
>i.e.

>Start Nightly System backup Sun May 17 22:55:03 EDT 1998
>Finish Nightly System backup Mon May 18 06:00:09 EDT 1998

>The number of fields in each line varies -- there are not always 6 fields
>before the actual hh:mm:ss record.

>Any way to use awk (or something else) to grab the 3rd field from the end of
>the line?  I tried something like awk '{(x=$NF - 3); print $x}' but couldn't
>get it.  (Please note that my syntax there is not correct, I know that.  I
>don't have the script in front of me.

x = $NF - 3 will set x to the value of the last field, then subtract 3
from it. You want to set:

x = NF - 3

--
Adam Lovick

 
 
 

Awk from right to left

Post by Jon LaBad » Wed, 20 May 1998 04:00:00


|> Help!  I have many (100's) of logfiles I am trying to analyze.  Each entry in
|> the logfile ends with the output from `date`
|> i.e.
|>
|> Start Nightly System backup Sun May 17 22:55:03 EDT 1998
|> Finish Nightly System backup Mon May 18 06:00:09 EDT 1998
|>
|> The number of fields in each line varies -- there are not always 6 fields
|> before the actual hh:mm:ss record.
|>
|> Any way to use awk (or something else) to grab the 3rd field from the end of
|> the line?  I tried something like awk '{(x=$NF - 3); print $x}' but couldn't
|> get it.  (Please note that my syntax there is not correct, I know that.  I
|> don't have the script in front of me.

Correct idea, wrong syntax.  As an example, two print fields in
reverse solutions:

  awk '
  {
        for (i = 0; i < NF; i++)
                print $(NF - i)         # note paren location
  }' datafile

  awk '
  {
        for (i = NF; i > 0; i--
                print $i
  }' datafile

jon
--


 4455 Province Line Road        (609) 252-0159
 Princeton, NJ  08540-4322      (609) 683-7220 (fax)

 
 
 

Awk from right to left

Post by Heiner Steve » Wed, 20 May 1998 04:00:00



> Any way to use awk (or something else) to grab the 3rd field from the end of
> the line?  I tried something like awk '{(x=$NF - 3); print $x}' but couldn't
> get it.  (Please note that my syntax there is not correct, I know that.  I
> don't have the script in front of me.

awk '{print $(NF-3)}'

Heiner
--
  --------------------------------------------------------------

/ Heiner's SHELLdorado: http://www.oase-shareware.org/shell  /
-------------------------------------------------------------
ZZW:q!!xx^K^DYES^MQXexit^Mquit^M^C^C^Z^?^\Qq^[xxxalles kacke

 
 
 

1. Left vs. Ctl-Left vs. Alt-Left

I'm running Red Hat Linux 4.1.  For Bash, I'd like to be able to associate
different commands to the left arrow key, ctl-left and alt-left.  Likewise for
the right arrow key.  Unfortunately, "cat -t" and "ctl-v" both report that all
variants of left generate the same keystroke;  likewise for right.  Is there any
way to generate a different command for each variant?

Dave Morgenlender

2. Solaris 2.6 on an Ultra10 will not boot

3. vi: !}fmt to flush left, left/right, right, middle

4. Books on writing drivers?

5. Netscape and ALT+Left,ALT+Right keys

6. Winfast S320 (RivaTNT chip) and X

7. How do you switch the left and right mouse button in linux ?

8. *** PLEASE READ THIS BEFORE POSTING *** (help-2.09)

9. X-win scrol-bar , right or left ?

10. What is the C++ equivalent of Left() and right() and mid()

11. Discriminating between left and right modifiers with xmodmap ?

12. Display right-left switched

13. Arithmetic in (t)csh : right to left ???