Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-28-2008, 09:55 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2008
Posts: 11
Rep:
|
script to check the last line in a file is blank line or not
hi ,
i just need to check that my last line at the end of the script is a blank line or not
iam trying to satisfy the if condition for this but i coudn't
code which i had tried is
blank = `tail -1 /home/oracle/grep.sh`
if [ "$blank" = '' ]
then
echo "end of the line is the blank line"
else
echo "error"
fi
please suggest
|
|
|
|
02-28-2008, 10:14 AM
|
#2
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
one way:
Code:
isblank=$(sed -n '$p' filename | grep -q '^$' && echo 'blank')
if [ $iblank = "blank" ] ; then
echo "is blank"
else
echo "is not blank"
fi
|
|
|
|
02-28-2008, 10:28 AM
|
#3
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
This is up to interpretation. Do you mean a blank line as far as what you'd see in an editor, or a blank line as far as having a line of zero length as read by a program? Normally when you put a newline at the end of the last line it looks like there's a blank line at the end in the editor, but in reality that line isn't there. Is that what you're talking about? A true blank line would look like two in an editor.
ta0kira
|
|
|
|
02-28-2008, 10:54 AM
|
#4
|
|
Member
Registered: Oct 2003
Distribution: Archlinux
Posts: 147
Rep:
|
Code:
awk 'END{print ($0=="" ? "blank" : "not blank")}' file
|
|
|
|
02-28-2008, 11:08 AM
|
#5
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
All you need to do with your code is change [ "$blank" = '' ] to [ -z "$blank" ].
ta0kira
|
|
|
|
02-29-2008, 06:42 PM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,801
|
Quote:
Originally Posted by naveensankineni
hi ,
i just need to check that my last line at the end of the script is a blank line or not
iam trying to satisfy the if condition for this but i coudn't
code which i had tried is
Code:
blank = `tail -1 /home/oracle/grep.sh`
if [ "$blank" = '' ]
then
echo "end of the line is the blank line"
else
echo "error"
fi
please suggest
|
Code:
blank = `tail -1 /home/oracle/grep.sh`
^ ^ remove these spaces
Code:
if [ "$blank" = '' ]
^^ put "" here
Your code will run fine.
jlinkels
|
|
|
|
02-29-2008, 07:02 PM
|
#7
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
Good call. I completely missed the spaces in the assignment.
ta0kira
|
|
|
|
02-29-2008, 09:23 PM
|
#8
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by naveensankineni
hi ,
i just need to check that my last line at the end of the script is a blank line or not
iam trying to satisfy the if condition for this but i coudn't
code which i had tried is
blank = `tail -1 /home/oracle/grep.sh`
if [ "$blank" = '' ]
then
echo "end of the line is the blank line"
else
echo "error"
fi
please suggest
|
blank line?
Code:
awk 'BEGIN{FS=""}END{ if ($NF ~ /[[:blank:]]/) print "blank"}' file
or
Code:
# b=`tail -1c file`
# if [ "$b" == " " ];then echo "blank"; fi
blank
|
|
|
|
02-29-2008, 11:21 PM
|
#9
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,234
|
What about (only) tabs - surely a check for whitespace would be better...
|
|
|
|
03-01-2008, 12:11 AM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
i think i have misunderstood the requirement. my bad.
|
|
|
|
03-01-2008, 11:13 PM
|
#11
|
|
LQ Newbie
Registered: Feb 2008
Posts: 11
Original Poster
Rep:
|
thanks to all you for your tremendous response .
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:56 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|