Printing the last line ONLY ? 
Author Message
 Printing the last line ONLY ?

Hi there...
I use a crontab job to print out an unknown number of lines to another
file every 5 minutes. It goes like this:
tail nohup.out > outfile.txt
The 'outfile.txt" should now look like this:
blah_blah_blah_1%
blah_blah_blah_2%
blah_blah_blah_3%
blah_blah_blah_4%
blah_blah_blah_5%

(It may have 1 line to 10 lines though, but it ALWAYS will have the
percent-sign in the last position of every line...)

Here's where I'm stuck:
I want to print the last line ONLY (in this example from above, it
should print out "blah_blah_blah_5%" and nothing else),
no matter how many line there is in 'outfile.txt', and I'm wondering if
AWK could do this easily, or maybe I don't have to use AWK at all?

Thanks in advance,
Jocke



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

Quote:

> [ ...snip ...]

> Here's where I'm stuck:
> I want to print the last line ONLY (in this example from above, it
> should print out "blah_blah_blah_5%" and nothing else),
> no matter how many line there is in 'outfile.txt', and I'm wondering if
> AWK could do this easily, or maybe I don't have to use AWK at all?

Here's a quick way with 'sed' ...

% sed -n -e '$p' filename

--
###############################
# Art Haas
# (713) 689-2417
###############################



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?


Quote:
>Hi there...
>I use a crontab job to print out an unknown number of lines to another
>file every 5 minutes. It goes like this:
>tail nohup.out > outfile.txt
>The 'outfile.txt" should now look like this:
>blah_blah_blah_1%
>blah_blah_blah_2%
>blah_blah_blah_3%
>blah_blah_blah_4%
>blah_blah_blah_5%

>(It may have 1 line to 10 lines though, but it ALWAYS will have the
>percent-sign in the last position of every line...)

>Here's where I'm stuck:
>I want to print the last line ONLY (in this example from above, it
>should print out "blah_blah_blah_5%" and nothing else),
>no matter how many line there is in 'outfile.txt', and I'm wondering if
>AWK could do this easily, or maybe I don't have to use AWK at all?

You're using tail already, so why not use it again:

tail -1 output.txt

will output the last line of output.txt

man tail

of course, if you'd done:

tail -1 nohup.out > output.txt

then output.txt would have just one line to begin with.  :-)

Chuck Demas
Needham, Mass.

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

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



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

Quote:

>> I want to print the last line ONLY (in this example from above, it
>> should print out "blah_blah_blah_5%" and nothing else),
>Here's a quick way with 'sed' ...
>% sed -n -e '$p' filename

or

tail -1 filename

but in awk

{ x = $0 } END { print x }

Cheers Terry...



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

<snip>

Quote:
>but in awk

>{ x = $0 } END { print x }

Awk brings the final $0 (and FNR, NR, etc.) into the END block, so just

END { print }



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

Quote:



><snip>

>>but in awk

>>{ x = $0 } END { print x }

>Awk brings the final $0 (and FNR, NR, etc.) into the END block, so just

>END { print }

Gawk and some other awk I have doesn't while mawk does. Stuck me as just
another  vagueness in the language - does anyone know if the last $0 read
is supposed to be available in and END statement?

Cheers Terry...



Wed, 13 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?


Quote:



> ><snip>

> >>but in awk

> >>{ x = $0 } END { print x }

> >Awk brings the final $0 (and FNR, NR, etc.) into the END block, so
just

> >END { print }

> Gawk and some other awk I have doesn't while mawk does. Stuck me as
just
> another  vagueness in the language - does anyone know if the last $0
read
> is supposed to be available in and END statement?

> Cheers Terry...

In gawk $0 should definately be available in the END rule. From the
manual:

"(...) Inside an END rule, what is the value of $0 and NF?
Traditionally, due largely to implementation issues, $0 and NF were
undefined inside an END rule. The POSIX standard specified that NF was
available in an END rule, containing the number of fields from the last
input record. Due most probably to an oversight, the standard does not
say that $0 is also preserved, although logically one would think that
it should be. In fact, gawk does preserve the value of $0 for use in
END rules. Be aware, however, that Unix awk, and possibly other
implementations, do not."

In any case I would definately say that I'm in favor of the "tail -1"
solution. The original poster already used tail and just need to add
the -1 arg to his script. Straight forward and highly portable.

/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 14 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

Quote:

> Hi there...
> I use a crontab job to print out an unknown number of lines to another
> file every 5 minutes. It goes like this:
> tail nohup.out > outfile.txt
> The 'outfile.txt" should now look like this:
> blah_blah_blah_1%
> blah_blah_blah_2%
> blah_blah_blah_3%
> blah_blah_blah_4%
> blah_blah_blah_5%

> (It may have 1 line to 10 lines though, but it ALWAYS will have the
> percent-sign in the last position of every line...)

> Here's where I'm stuck:
> I want to print the last line ONLY (in this example from above, it
> should print out "blah_blah_blah_5%" and nothing else),
> no matter how many line there is in 'outfile.txt', and I'm wondering if
> AWK could do this easily, or maybe I don't have to use AWK at all?

> Thanks in advance,
> Jocke

You could use sed

        sed -n '$p' filename > filename.new

-n   suppresses output
$    matches the last line
p    prints the matched line

It is most likely to be quicker than awk.

        --Yunus
-------------------------------------------------------------
"Don't be suckered in by the comments - debug only the code."
To reply by e-mail replace 'lovelyspam' with 'Yunus'.
-------------------------------------------------------------



Fri, 15 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?

Quote:


> > Hi there...
> > I use a crontab job to print out an unknown number of lines to another
> > file every 5 minutes. It goes like this:
> > tail nohup.out > outfile.txt
> > The 'outfile.txt" should now look like this:
> > blah_blah_blah_1%
> > blah_blah_blah_2%
> > blah_blah_blah_3%
> > blah_blah_blah_4%
> > blah_blah_blah_5%

> > (It may have 1 line to 10 lines though, but it ALWAYS will have the
> > percent-sign in the last position of every line...)

> > Here's where I'm stuck:
> > I want to print the last line ONLY (in this example from above, it
> > should print out "blah_blah_blah_5%" and nothing else),
> > no matter how many line there is in 'outfile.txt', and I'm wondering if
> > AWK could do this easily, or maybe I don't have to use AWK at all?

> > Thanks in advance,
> > Jocke

> You could use sed

>         sed -n '$p' filename > filename.new

> -n   suppresses output
> $    matches the last line
> p    prints the matched line

> It is most likely to be quicker than awk.

what about tail -1 filename.

Probably quicker than sed.

John

Quote:

>         --Yunus
> -------------------------------------------------------------
> "Don't be suckered in by the comments - debug only the code."
> To reply by e-mail replace 'lovelyspam' with 'Yunus'.
> -------------------------------------------------------------

--

Philips Semiconductors - CD Systems

Tel. +44 (0) 23 80312 667
FAX. +44 (0) 23 80316 304



Fri, 15 Mar 2002 03:00:00 GMT  
 Printing the last line ONLY ?
On Mon, 27 Sep 1999 12:40:12 GMT, John Ireland

Quote:



>> > Hi there...
>> > I use a crontab job to print out an unknown number of lines to another
>> > file every 5 minutes. It goes like this:
>> > tail nohup.out > outfile.txt
>> You could use sed
>> It is most likely to be quicker than awk.

>what about tail -1 filename.

>Probably quicker than sed.

Sometimes tail'ing a growing file like a nohup.out file can result in
partial 'last' lines -- lines that will be fully written in another
few microseconds.  Lines that won't even be the 'last' lines in
another few microseconds.

So I concur with using 'tail' versus 'sed' or 'awk' to get near the
last lines without passing the entire nohup.out file.  I usually find
the problem to be to find the last complete set of lines in a
"nohup.out" file.  But to honor the simpler request of the original
post and try to make it a llittle more robust by demanding the 'last'
complete line, could we wave our hands a little bit about what
constitutes the 'last' line and give the a 'pretty close to the last
complete line' by processing the first line from 'tail -2'?:

    tail -2 nohup.out  |  awk 'NR==1 {  process it }'



Mon, 18 Mar 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. print last line of input file

2. print the last field on a line

3. print last field in a record, 2nd last.

4. Search file and print line + next line?

5. Printing line by line

6. The pretty-printer, *print-lines* and per-line prefixes

7. Print formatting - same line printing?

8. Newbie: dots ending last line of a method

9. Pattern for the last 2 lines

10. read the last line in file

11. join last 2 lines of a file

12. deleting newline from last line in file

 

 
Powered by phpBB® Forum Software