LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-26-2014, 02:27 AM   #1
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Rep: Reputation: Disabled
reading second last line from a file and save in variable


I have a file say 'fileX' which consists of 'n' number of lines. I want to read just the second last line from the file and store it in a variable. Can someone plz help me with the code.
Thanks in advance.
 
Old 08-26-2014, 02:34 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
Is this a homework assignment by any chance. Why not show us how far you've gone and what problems you are encountering. It will be easier to help you when we see what your problem is.
jdk
 
Old 08-26-2014, 07:01 AM   #3
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
I was thinkin abt checking the number of lines in the file using the 'wc -l' command and storing the value in a variable. And then (n-1)th value using 'sed' would hopefully go to the correct line and give me the required answer.
Is there any easier way to do it?
 
Old 08-26-2014, 07:14 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Can't you Google it? Just did it and there are lots of examples how to display last but one Line. Some ways involve sed only; some of them the combination of head and tail. There is no need to count the number of lines.
 
Old 08-26-2014, 07:35 AM   #5
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
I've Googled a lot.. and found many examples to display value at a specific line number. But in my case, the lines keep on increasing in the file and the line number is not known for the second last value.
 
Old 08-26-2014, 07:46 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
As suggested, search "last but one", not "second last" - the google engine seems to handle the former better.
 
Old 08-26-2014, 08:00 AM   #7
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
tail -2 yourfile | head -1
works.. Thanks for the help.
 
  


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] Reading line and assign multidimensional variable Alkass Linux - Newbie 1 12-15-2012 09:01 PM
reading variable while remaining on the same line - bash GSMD Programming 3 04-24-2011 10:47 AM
[SOLVED] Reading file into variable in bash ted_chou12 Linux - Newbie 3 03-21-2011 02:52 AM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 01:24 AM
how to cat a text file and save it as a variable mrobertson Programming 37 07-05-2005 09:20 AM


All times are GMT -5. The time now is 01:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration