LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 11: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, 12:18 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 12:24 PM   #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, 12:26 PM   #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, 12:27 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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 12:30 PM.
 
Old 11-16-2011, 01: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, 01:09 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
What system are you running this on?
 
Old 11-16-2011, 01: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, 01: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, 01:17 PM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Does that work ?
Code:
sed -n '$p' <filename>
 
Old 11-16-2011, 01: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, 01:43 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 02:04 PM.
 
1 members found this post helpful.
Old 11-17-2011, 09: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, 09:56 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 10: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:02 AM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-05-2010 12:38 AM
tail file if the name is changeable m1n Linux - Newbie 7 01-01-2010 08:09 AM
Cannot use tail -f to monitor log file v0nd00 Ubuntu 7 12-11-2009 03:05 AM
tail file over tcp lloydie-t Linux - Networking 5 05-15-2007 03:18 AM


All times are GMT -5. The time now is 02:18 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration