LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   tail the last line of a file (https://www.linuxquestions.org/questions/linux-newbie-8/tail-the-last-line-of-a-file-913858/)

Pinglu 11-16-2011 10:47 AM

tail the last line of a file
 
Hi,

In my current unix server, if I need to tail the last line of a file, I need to type tail -0 <filename> as opposed to tail -1 <filename>. I'm wondering if it is because of the Linux kernel version. Do different shells have different behavior, e.g. Korn Shell and Bash.

Thanks in advance.

suicidaleggroll 11-16-2011 11:18 AM

It's probably due to your version of tail. Does your tail not have the -n flag? That's generally what I use:

Code:

tail -n 1 <filename>

Pinglu 11-16-2011 11:24 AM

Quote:

Originally Posted by suicidaleggroll (Post 4525669)
It's probably due to your version of tail. Does your tail not have the -n flag? That's generally what I use:

Code:

tail -n 1 <filename>

tail -n 0 also gives me the same result. So there's no difference using -n flag or not. If I type tail -n 1 <filename>, it will gives me last two lines.

kbscores 11-16-2011 11:26 AM

I'd imagine they'd all be 0 and it is most likely determined by command itself. The only difference would most likely be how lines are determined. Not to mention most programming counters start w/0, unless purposefully skipped.

suicidaleggroll 11-16-2011 11:27 AM

What distro are you using and what is your version of tail?

Code:

tail --version
I've tried on RHEL 4 (tail 5.2.1), OpenSUSE 11.4 (tail 8.9), every Fedora from 4 to 15 (tail 5.2.1 to 8.10), and cygwin with tail 6.10, and can't reproduce that behavior.

Pinglu 11-16-2011 12:07 PM

Quote:

Originally Posted by suicidaleggroll (Post 4525683)
What distro are you using and what is your version of tail?

Code:

tail --version
I've tried on RHEL 4 (tail 5.2.1), OpenSUSE 11.4 (tail 8.9), every Fedora from 4 to 15 (tail 5.2.1 to 8.10), and cygwin with tail 6.10, and can't reproduce that behavior.



$tail --version
Usage: tail [-f] [-c number|-n number|-m number|-b number|-k number] [file]
Usage: tail [-r] [-n number] [file]
Usage: tail [+|-[number]][l|b|c|k|m][f] [file]

suicidaleggroll 11-16-2011 12:09 PM

What system are you running this on?

Pinglu 11-16-2011 12:10 PM

Quote:

Originally Posted by kbscores (Post 4525682)
I'd imagine they'd all be 0 and it is most likely determined by command itself. The only difference would most likely be how lines are determined. Not to mention most programming counters start w/0, unless purposefully skipped.

The file lines are ended by unix end line as normal. I tried "head -0 <filename>" which gives me nothing and "head -1 <filename>" gives me the first line.

Pinglu 11-16-2011 12:11 PM

Quote:

Originally Posted by suicidaleggroll (Post 4525727)
What system are you running this on?

It's KORN SHELL. AIX machine

Cedrik 11-16-2011 12:17 PM

Does that work ?
Code:

sed -n '$p' <filename>

Pinglu 11-16-2011 12:42 PM

Quote:

Originally Posted by Cedrik (Post 4525739)
Does that work ?
Code:

sed -n '$p' <filename>

Yes, it works, display the last line. Here my question is when I'm using tail, why do I need to specify the -n option as 0 as opposed to 1.

David the H. 11-16-2011 12:43 PM

The differences are mostly due to the source of the basic system utilities, rather than the shell* or the system.

I believe most of the traditional flavors of Unix provide their own home-spun implementations of the basic core utilities, while Linux distros pretty much all use the ones written by the gnu organization.

While the command names and basic posix-defined functions should be the same across all *nixes, they can sometimes be quite different in their operating details and the exact mix of features they support. The gnu ones in particular have often been greatly expanded with additional features not available elsewhere (e.g. the -i in-file-editing option in sed).


*Although there are syntax differences between bash and ksh, for example, each shell will generally work the same across all platforms, barring the occasional feature that isn't supported by some underlying system.


Edit: Here are the manpage for aix and the gnu documentation, so you can compare their features. They don't appear to say anything about using 0 vs 1, however.

http://publib.boulder.ibm.com/infoce...cmds5/tail.htm

http://www.gnu.org/software/coreutil...nvocation.html

kbscores 11-17-2011 08:49 AM

Hmmmm interesting about head. If I think about it -- it probably uses Line 0 POS 0 = 0 Where as if fully reversed Line 0 POS EOF = 0. Which would make sense head vs tail.

suicidaleggroll 11-17-2011 08:56 AM

Quote:

Originally Posted by kbscores (Post 4526547)
Hmmmm interesting about head. If I think about it -- it probably uses Line 0 POS 0 = 0 Where as if fully reversed Line 0 POS EOF = 0. Which would make sense head vs tail.

Except that with head/tail you aren't telling it to print "line 1", you're telling it to print "1 line". Big difference. Telling it to print "0 lines" is nonsense and shouldn't do anything (which it doesn't on any GNU version of tail). It sounds like the version of tail he's working with has a bug, or maybe the developer of it just has a different interpretation of "print 0 lines" and "print 1 line" than the rest of the world.

kbscores 11-17-2011 09:09 AM

Ok - I see now - ty for explanation.


All times are GMT -5. The time now is 03:25 AM.