Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Empty lines in input file

Status
Not open for further replies.

TanyaB

Programmer
Aug 15, 2001
18
IL
Hi,
I have a KSH script that read lines from input file and process them one by one:
while read line ; do
##############
done < $ConfigFile

I want to make sure, that input line is not comment (#) or empty line; for comment I've come up with
if echo $line | grep '^\#' >/dev/null ;then
#Comment Line
else
#regular line - process
fi

How can I detect an empty line?
I've tried something like
if echo $line | grep '\^\$' >/dev/null ;then ...
But it didn't worked - it took every line...
 
Tanya,

Try changing your grep statement from '\^\$' > /dev/null to

grep '^$' > /dev/null.


Steve
 
I might try something like this - to remove all the blank and comment lines in one go

Code:
egrep -v '^$|^#' $ConfigFile | while read line ; do
  # regular line - process
done

Depends what you mean by a blank line. Some blank lines could consist entirely of spaces and tabs, but would not be caught by that egrep command (though its easy to add).
Additionally,
[tt]hello # this is a comment[/tt]
is not detected either at present, since comments are assumed to begin at the start of a line.

--
 
Another way to ignore comments and empty/blank lines:
Code:
tab=`echo &quot;\t\c&quot;`
sed -e 's/#.*$//' -e '/^[ '$tab']*$/d' $ConfigFile |
 while read line
 do
  #regular line - process
 done
Anyway, to test an empty variable:
Code:
[ -z &quot;$line&quot; ] && echo empty


Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top