User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 427,787 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,825 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 585 | Replies: 6
Reply
Join Date: Nov 2007
Posts: 40
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

print only the last entry of the file

  #1  
Jul 24th, 2008
i wanted to printout the last entry of the file without passing in any arguments but how can i able to do it? i know how to print out where argument is supplied and doing a grep to match it.

example inside the file i have:
apple:2:1:3
orange:1:2:3
grape:2:3:4

i wanted only to print out(last entry of my file):
grape:2:3:4
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: print only the last entry of the file

  #2  
Jul 24th, 2008
sed -n '$p' filename
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: print only the last entry of the file

  #3  
Jul 24th, 2008
thanks masijade! thanks for ur help!!!
Reply With Quote  
Join Date: Dec 2005
Posts: 3,640
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: print only the last entry of the file

  #4  
Jul 24th, 2008
Or
tail -1 file
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Apr 2008
Posts: 39
Reputation: omrsafetyo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
omrsafetyo omrsafetyo is offline Offline
Light Poster

Re: print only the last entry of the file

  #5  
Jul 24th, 2008
^ Nice and simple, just the way I like it... though I'm more interested in the sed example.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: print only the last entry of the file

  #6  
Jul 25th, 2008
Originally Posted by Salem View Post
Or
tail -1 file


True. I like to give somewhat obscure answers to things that I belive are probably/possibly homework questions. That way, if they are not a homework question, the OP has something (s)he can use, and if they are, the teacher knows, as soon as (s)he sees it, that the student probably did not come up with it themselves. ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2006
Posts: 140
Reputation: ghostdog74 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 26
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: print only the last entry of the file

  #7  
Jul 25th, 2008
sed will be slower than tail for huge files. Just a tip
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC