LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2011, 10:47 AM   #1
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Rep: Reputation: Disabled
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.
 
Old 11-16-2011, 11:18 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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>
 
Old 11-16-2011, 11:24 AM   #3
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 11-16-2011, 11:26 AM   #4
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
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.
 
Old 11-16-2011, 11:27 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.

Last edited by suicidaleggroll; 11-16-2011 at 11:30 AM.
 
Old 11-16-2011, 12:07 PM   #6
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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]
 
Old 11-16-2011, 12:09 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What system are you running this on?
 
Old 11-16-2011, 12:10 PM   #8
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kbscores View Post
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.
 
Old 11-16-2011, 12:11 PM   #9
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
What system are you running this on?
It's KORN SHELL. AIX machine
 
Old 11-16-2011, 12:17 PM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Does that work ?
Code:
sed -n '$p' <filename>
 
Old 11-16-2011, 12:42 PM   #11
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cedrik View Post
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.
 
Old 11-16-2011, 12:43 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 11-16-2011 at 01:04 PM.
 
1 members found this post helpful.
Old 11-17-2011, 08:49 AM   #13
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
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.
 
Old 11-17-2011, 08:56 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by kbscores View Post
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.
 
Old 11-17-2011, 09:09 AM   #15
kbscores
Member
 
Registered: Oct 2011
Location: USA
Distribution: Red Hat
Posts: 259
Blog Entries: 9

Rep: Reputation: 32
Ok - I see now - ty for explanation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] tail command syntax to print a sequence of line rhadmn Linux - General 6 12-22-2010 06:02 AM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
tail file if the name is changeable m1n Linux - Newbie 7 01-01-2010 07:09 AM
Cannot use tail -f to monitor log file v0nd00 Ubuntu 7 12-11-2009 02:05 AM
tail file over tcp lloydie-t Linux - Networking 5 05-15-2007 02:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration