ignore blank lines in while loop

Hi,

i am having one text file it contains some blank lines and i want to ignore that blank lines .

 
#! /bin/bash
clear
rdCount=0;
while read myline
do
  echo $myline
  let rdCount=$rdCount+1
done < ps.txt
echo "Total line=$rdCount"
 

and ps .txt contains the data-

ps.txt
 
a
b
 
c
d
e
 
 
 
 

in above code i want to ignore the blank lines and finally the output should be 5.
Please help me to do so

Thanks

Hi,
Please try with this

#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ -z "${myline}" ]; then
    echo "line is empty"
 else
  echo $myline
  let rdCount=$rdCount+1
 fi
done < ps.txt
echo "Total line=$rdCount"  

thanks,
venkat

In your example data a "blank" line consists of a single space character.

#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ "${myline}" = " " ]
  then
        continue
  fi
  echo $myline
  let rdCount=$rdCount+1
done < ps.txt
echo "Total line=$rdCount"  
$ perl -ne '(!/^\s+$/) && $c++; END{print "$c\n"}' ps.txt
5
$ grep -c . ps.txt

hi all,

i have tried above code , its working fine for few file but if my ps.txt file contains the diffent data please find the attachment it gives me the output 6 instead of 3 .

please help me to solve this

Hi,
when i run the script w.r.t to this code it is reporting 3 only

$ sh script.sh
line is empty
line is empty
line is empty
Total line=3
$ cat script.sh
#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ -z "${myline}" ]; then
    echo "line is empty"
 else
 # echo $myline
  let rdCount=$rdCount+1
 fi
done < ps.txt
echo "Total line=$rdCount"

thanks,
venkat

@aish11, did u try with my code .. It gives me the expected output ..

i have tried again ...where i am wrong. please see the below code, txt file and output carefully

code-

#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ -z "${myline}" ]; then
    echo "line is empty"
 else
 # echo $myline
  let rdCount=$rdCount+1
 fi
done < psp.txt
echo "Total line=$rdCount"

textfile-psp.txt

aaa123,null,G,E,null,1,65,54,56,GFD,P,3443,567889,tet4545,null,null,null,null,null,null,null,null,23,9090,ryrt,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,90545,null,CS,null,null,null,null,null,null,null,null,null,null,ftyrtr,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,TRUE,TRUE
 
aaa124,null,G,E,null,1,65,54,56,GFD,P,3443,567889,tet4545,null,null,null,null,null,null,null,null,23,9090,ryrt,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,90545,null,CS,null,null,null,null,null,null,null,null,null,null,ftyrtr,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,TRUE,TRUE
 
bbb125,null,G,E,null,1,65,54,56,GFD,P,3443,567889,tet4545,null,null,null,null,null,null,null,null,23,9090,ryrt,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,90545,null,CS,null,null,null,null,null,null,null,null,null,null,ftyrtr,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,TRUE,TRUE

output

Total line=6

Hi,
it looks in space is counted as string when we use -z it checks for size

$ uname
CYGWIN_NT-5.1

btw, your OS

thanks,
venkat

Linux

bash-3.2$ sh sc.sh
line is empty
line is empty
line is empty
line is empty
Total line=3
bash-3.2$ uname
Linux
bash-3.2$ cat sc.sh
#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ -z "${myline}" ]; then
    echo "line is empty"
 else
 # echo $myline
  let rdCount=$rdCount+1
 fi
done < tes.txt
echo "Total line=$rdCount"

in my Linux environment its reporting correctly

thanks,
venkat

Your new data contains some totally blank lines.
The best idea is to filter them out at the earliest opportunity.
This "sed" removes lines which contain only a text file line terminator.
For good practice always put quotes round string variables. Also there is never a reason to end a Shell line with a semi-colon.

#! /bin/bash
clear
rdCount=0
sed -e "/^$/d" ps.txt | while read myline
do
  echo "${myline}"
  let rdCount=$rdCount+1
done
echo "Total line=$rdCount"

instead of while loop use for loop
it will slove th problem...

try this

 awk ' $0 !~ /^[[:space:]]*$/{a++} END{print a}' filename

Thanks tarun...its working but can u plz help me using if else command.

because if line is blank i want to ignore it . and if line contains some data i want apply some functions on it.

Thanks

try this

while read line
do
(echo $line | egrep -q "^[[:space:]]$" ) && echo "do your operation"
done < filename

replace echo "do your operation" with command you want to execute for non-blank lines

Hi ALL,

whatever the script i have tried before

#! /bin/bash
clear
rdCount=0;
while read myline
do
  if [ -z "${myline}" ]; then
    echo "line is empty"
  else
   echo $myline
   let rdCount=$rdCount+1
  fi
done < ps.txt
echo "COUNT=$rdCount"
 
 

some times it working some times not.I think their might b problem of unix new line charecter.

but i dont know excatly what is that .... can anyone explain me that??