LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   script to check the last line in a file is blank line or not (http://www.linuxquestions.org/questions/programming-9/script-to-check-the-last-line-in-a-file-is-blank-line-or-not-624500/)

naveensankineni 02-28-2008 10:55 AM

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

jim mcnamara 02-28-2008 11:14 AM

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


ta0kira 02-28-2008 11:28 AM

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

angrybanana 02-28-2008 11:54 AM

Code:

awk 'END{print ($0=="" ? "blank" : "not blank")}' file

ta0kira 02-28-2008 12:08 PM

All you need to do with your code is change [ "$blank" = '' ] to [ -z "$blank" ].
ta0kira

jlinkels 02-29-2008 07:42 PM

Quote:

Originally Posted by naveensankineni (Post 3072732)
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

ta0kira 02-29-2008 08:02 PM

Good call. I completely missed the spaces in the assignment.
ta0kira

ghostdog74 02-29-2008 10:23 PM

Quote:

Originally Posted by naveensankineni (Post 3072732)
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


syg00 03-01-2008 12:21 AM

What about (only) tabs - surely a check for whitespace would be better...

ghostdog74 03-01-2008 01:11 AM

i think i have misunderstood the requirement. my bad.

naveensankineni 03-02-2008 12:13 AM

thanks to all you for your tremendous response .


All times are GMT -5. The time now is 10:01 PM.