print last line of input file 
Author Message
 print last line of input file

Hi,

I am trying to edit a lot of files, of different sizes. As part of this,
I need to print the first 3 lines and the last line of each file.

To print the first 3 lines I have used:

NR==1 || NR==2 || NR==3 {print $0}

Does anyone know of an easy way to print the last line please?

Thanks,

Jane

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



Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file
For printing first three lines and the last one
you can use this program

awk '
  NR <= 3{
    print
    last = $0
  }
  END{
    if (FNR > 0)
      print last
  }
'



Quote:
> Hi,

> I am trying to edit a lot of files, of different sizes. As part of
this,
> I need to print the first 3 lines and the last line of each file.

> To print the first 3 lines I have used:

> NR==1 || NR==2 || NR==3 {print $0}

> Does anyone know of an easy way to print the last line please?

> Thanks,

> Jane

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

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


Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file

Quote:

> Hi,

> I am trying to edit a lot of files, of different sizes. As part of this,
> I need to print the first 3 lines and the last line of each file.

> To print the first 3 lines I have used:

> NR==1 || NR==2 || NR==3 {print $0}

> Does anyone know of an easy way to print the last line please?

Jane,

As the last line read in remains in $0 in the END block
you could just use:

gawk 'END{print}' file

Actually, thinking about it, I'm not sure if this works
in awk or nawk - you may have to use:

awk '{last=$0};END{print last}' file

Hope this helps,

Tristan.



Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file

Quote:

> awk '
>   NR <= 3{

I think this should be FNR - Jane wants to print the first 3 lines of
each file.

Quote:


> > I am trying to edit a lot of files, of different sizes. As part
> > ofthis, I need to print the first 3 lines and the last line of
> > each file.

--

"... We work by wit, and not by witchcraft;
 And wit depends on dilatory time."


Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file

Quote:

> gawk 'END{print}' file

> Actually, thinking about it, I'm not sure if this works
> in awk or nawk

The gawk info pages discuss this in detail. To quote:

"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."

Another point, also discussed in gawk info:

"Many long time `awk' programmers use `print' in `BEGIN' and `END'
rules, to mean `print ""', relying on `$0' being null.  While you
might generally get away with this in `BEGIN' rules, in `gawk' at
least, it is a very bad idea in `END' rules.  It is also poor style,
since if you want an empty line in the output, you should say so
explicitly in your program."

Hope it helps.

--

"... We work by wit, and not by witchcraft;
 And wit depends on dilatory time."



Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file
Two comments:

1. As someone stated, use FNR, not NR.

2. In END, use FNR>3 so the last line is not printed
twice if the file has 3 or less lines (if that's what you want).

Quote:

> For printing first three lines and the last one
> you can use this program

> awk '
>   NR <= 3{
>     print
>     last = $0
>   }
>   END{
>     if (FNR > 0)
>       print last
>   }
> '



> > Hi,

> > I am trying to edit a lot of files, of different sizes. As part of
> this,
> > I need to print the first 3 lines and the last line of each file.

> > To print the first 3 lines I have used:

> > NR==1 || NR==2 || NR==3 {print $0}

> > Does anyone know of an easy way to print the last line please?

> > Thanks,

> > Jane

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

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



Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file

Quote:

> Hi,

> I am trying to edit a lot of files, of different sizes. As part of this,
> I need to print the first 3 lines and the last line of each file.

> To print the first 3 lines I have used:

> NR==1 || NR==2 || NR==3 {print $0}

> Does anyone know of an easy way to print the last line please?

> Thanks,

> Jane

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

Do you have to use awk?  How about sed?

sed '1,3p;$p' filename

Hope that helps

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Sat, 10 May 2003 03:00:00 GMT  
 print last line of input file

Quote:



> > > I am trying to edit a lot of files, of different sizes. As part
> > > ofthis, I need to print the first 3 lines and the last line of
> > > each file.

> > awk '
> >   NR <= 3{

> I think this should be FNR - Jane wants to print the first 3 lines of
> each file.

Good observation. But what about the last line of each file?

--
Jim Monty

Tempe, Arizona USA



Sun, 11 May 2003 08:09:14 GMT  
 print last line of input file

Quote:



> > > I am trying to edit a lot of files, of different sizes. As part of this,
> > > I need to print the first 3 lines and the last line of each file.

> > awk '
> >   NR <= 3{
> >     print
> >     last = $0
> >   }
> >   END{
> >     if (FNR > 0)
> >       print last
> >   }
> > '

> Two comments:

Only two?

Quote:
> 1. As someone stated, use FNR, not NR.

> 2. In END, use FNR>3 so the last line is not printed
> twice if the file has 3 or less lines (if that's what you want).

Two good comments. But what about printing the last line of the
first file through the penultimate file where the number of files
is greater than one?

In rare fashion, Jane stated her problem precisely and unambiguously.
(See above.) She, more than most inquirers, deserves a whole answer.

--
Jim Monty

Tempe, Arizona USA



Sun, 11 May 2003 08:23:05 GMT  
 print last line of input file

Quote:


> > I am trying to edit a lot of files, of different sizes. As part of this,
> > I need to print the first 3 lines and the last line of each file.

> > To print the first 3 lines I have used:

> > NR==1 || NR==2 || NR==3 {print $0}

> > Does anyone know of an easy way to print the last line please?

> Do you have to use awk?  How about sed?

> sed '1,3p;$p' filename

This will print the fourth through the second-to-last lines once,
and the first, second, third, and last lines twice.

I think you meant to suggest

  sed -n '1,3p;$p' filename

And this works well inside of a shell loop. Here's a purposefully
oversimplified example:

  for file
  do
    sed -n '1,3p;$p' $file
  done

This sed script will, of course, print the last line of any file
containing less than four lines twice.

--
Jim Monty

Tempe, Arizona USA



Sun, 11 May 2003 08:35:09 GMT  
 print last line of input file

Quote:

> I am trying to edit a lot of files, of different sizes. As part of this,
> I need to print the first 3 lines and the last line of each file.

> To print the first 3 lines I have used:

> NR==1 || NR==2 || NR==3 {print $0}

> Does anyone know of an easy way to print the last line please?

Apparently not.

It takes too much chicanery to do get this right in awk. Use Perl instead:

$ cat 123last
#!/usr/local/bin/perl -w

while (<>) {
    print if $. <= 3;

Quote:
} continue {

    if (eof) {
        print if $. > 3;
        close ARGV;
    }
Quote:
}

$ cat file1
file1 line1
file1 line2
file1 line3
file1 line4
file1 line5
file1 line6
$ cat file2
file2 line1
file2 line2
$ cat file3
file3 line1
file3 line2
file3 line3
$ ./123last file1 file2 file3
file1 line1
file1 line2
file1 line3
file1 line6
file2 line1
file2 line2
file3 line1
file3 line2
file3 line3
$

--
Jim Monty

Tempe, Arizona USA



Sun, 11 May 2003 09:08:24 GMT  
 print last line of input file

{
    if (FNR <= 3) {
        if (FNR == 1 && fnr > 3) {
            print line
        }

        print
    }

    fnr  = FNR
    line = $0

Quote:
}

END {
    if (fnr > 3) {
        print line
    }
Quote:
}



Sun, 11 May 2003 10:06:05 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. print the last field on a line

2. Printing the last line ONLY ?

3. Search file and print line + next line?

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

5. read the last line in file

6. join last 2 lines of a file

7. deleting newline from last line in file

8. how to specify last line of file ?

9. How to read the x last lines of a text file

10. How to read the last line of http:// file

11. File: How to read the last line?

12. Best way to get Last line of file?

 

 
Powered by phpBB® Forum Software