LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-28-2008, 09:55 AM   #1
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Rep: Reputation: 0
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
 
Old 02-28-2008, 10:14 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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
 
Old 02-28-2008, 10:28 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 02-28-2008, 10:54 AM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
awk 'END{print ($0=="" ? "blank" : "not blank")}' file
 
Old 02-28-2008, 11:08 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
All you need to do with your code is change [ "$blank" = '' ] to [ -z "$blank" ].
ta0kira
 
Old 02-29-2008, 06:42 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by naveensankineni View Post
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
 
Old 02-29-2008, 07:02 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Good call. I completely missed the spaces in the assignment.
ta0kira
 
Old 02-29-2008, 09:23 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by naveensankineni View Post
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
 
Old 02-29-2008, 11:21 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
What about (only) tabs - surely a check for whitespace would be better...
 
Old 03-01-2008, 12:11 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i think i have misunderstood the requirement. my bad.
 
Old 03-01-2008, 11:13 PM   #11
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Original Poster
Rep: Reputation: 0
thanks to all you for your tremendous response .
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 04:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration