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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
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: 3,078
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: 3,078
Rep: 
|
All you need to do with your code is change [ "$blank" = '' ] to [ -z "$blank" ].
ta0kira
|
|
|
02-29-2008, 06:42 PM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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: 3,078
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,697
|
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: 21,437
|
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,697
|
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 .
|
|
|
All times are GMT -5. The time now is 01:06 PM.
|
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
|
|