LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-24-2013, 01:35 AM   #1
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Rep: Reputation: Disabled
Exclamation How to read 2nd to last line using head or tail command


Hello Everyone!!

Suppose a file has got 20 lines of text.
I want to display 2nd line to last line (don't want
the 1st line). How can i achieve this using head or tail command ?

Thanks in advance..
 
Old 09-24-2013, 01:46 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
As head and tail print out 10 lines per default you need the -n option. As you want all but the first line subtract 1 from 20 and give this to the -n option as an argument.

In case you need to find out how much lines a file has use wc -l <filename>.

Last edited by zhjim; 09-24-2013 at 01:50 AM.
 
Old 09-24-2013, 02:03 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I guess it depends

if you always want line 19 of a 20 line file then this will do it

Code:
awk '{if ( NR == "19" ) print}' InputFile
you could also use a variable
e.g.
Code:
IwantLine=19
awk '{if ( NR == '${IwantLine}' ) print}' InputFile
but a simple head | tail

Code:
tail -n2 InputFile | head -n1
It really depends on why you want "line 19"

lets pretend line 19 as the string, FooBar in it
Code:
grep "FooBar" InputFile
or
Code:
awk '/FooBar/{print}' InputFile
awk is more useful if you want/need to manipulate the line in anyway, maybe you only want certain 'fields'

if you give more 'context' ( sample input, what your goal is ) we can give you better guidance.
 
Old 09-24-2013, 02:50 AM   #4
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Smile

Thanks everyone for ur replies.. i will check on my system and revert back with results..
Thanks again...
 
Old 09-24-2013, 02:53 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by techie_san778 View Post
Suppose a file has got 20 lines of text.
I want to display 2nd line to last line (don't want
the 1st line). How can i achieve this using head or tail command ?
This will show the last 19 lines of a file:
Code:
tail -19n infile
The above is handy when you know the amount of lines in a file, if you don't know then sed might be better to use. The following prints from line 2 to the end of the file:
Code:
sed -n '2,$p' infile
The 2,$ is a line range and the $ is special and denotes the last line.
 
Old 09-24-2013, 03:01 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or simply use the -n +K option of tail:
Code:
tail -n +2 file
 
1 members found this post helpful.
Old 09-24-2013, 03:11 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783


I think I need more coffee

I read it as "second to last line", as in the line before the last
 
Old 09-29-2013, 08:09 AM   #8
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
@colucix : Yes, i also found out this command to be the most appropriate !!
 
  


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
LXer: Use head and tail to read text files LXer Syndicated Linux News 0 09-07-2012 01:40 AM
[SOLVED] Tail/head JJ Linux Linux - Newbie 4 02-11-2011 10:06 AM
[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
Command to separate 2nd line's 3rd word from a text file senthilvael Linux - Newbie 7 11-29-2007 11:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:22 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
Open Source Consulting | Domain Registration