The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
delete blank space in begining of line karthikn7974 Shell Programming and Scripting 4 05-07-2008 04:40 AM
Blank line ? varungupta UNIX for Advanced & Expert Users 2 09-10-2007 12:52 PM
insert text and add blank line athidhi Shell Programming and Scripting 7 08-16-2007 11:56 AM
adding blank line in egrep antalexi UNIX for Dummies Questions & Answers 2 05-24-2004 12:40 PM
Insert blank line if grep not found michieka Shell Programming and Scripting 6 01-05-2004 02:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-17-2008
tqlam tqlam is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 12
How to get last non-blank line?

Can someone here show how to get the last non-blank line from a text file?

Thank you!
  #2 (permalink)  
Old 01-17-2008
prash184u prash184u is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 23
Many ways

sed '$!d' file.txt

tail -1 file.txt
  #3 (permalink)  
Old 01-17-2008
tqlam tqlam is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 12
(sed '$!d' file.txt) doesn't work for me, it returns "sed: Extra characters after command"

(tail -1 file.txt) only give me the last line of the file.txt, but last line or last few lines in file.txt could be empty.

So, I need a way to extract the line in file.txt that has some characters.
  #4 (permalink)  
Old 01-17-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk 'NF{s=$0}END{print s}' file
Regards
  #5 (permalink)  
Old 01-17-2008
tqlam tqlam is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 12
That's what I got from awk by running your command:
awK: syntax error near line 1
awk: bailing out near line 1
  #6 (permalink)  
Old 01-17-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Use nawk or /usr/xpg4/bin/awk on Solaris ...
  #7 (permalink)  
Old 01-17-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Not the most eligant but it works:

Code:
sed -e '/^[<blank><tab>]*$/d' textfile | sed -n -e '$p'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0