i have tried the below code for counting the number of lines present in text file ignoring blank lines
#! /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 < ps5.txt
echo "COUNT=$rdCount"
and my ps5.txt contains data-
a
s
c
its giving me the correct output i.e
a
line is empty
line is empty
line is empty
s
line is empty
line is empty
line is empty
c
COUNT=3
now i have edited this file ,nw ps5.txt contains
hkj
h
h
h
when i have run the above script its giving me the wrong output i.e
hkj
h
h
h
COUNT=6
i guess their might b a problm of new line charecter but i dont know how to solve this.
In vi, type the following :%s/ at this point, press ctrl key, press v, release v, press m, release m and ctrl. Now continue the substitution command by further typing //g
The longer-term solution is to always use Text mode FTP when transferring text files from a Microsoft platform to a unix platform. A Microsoft text file has every line terminated with two characters "carriage-return" then "line-feed", whereas a unix text file has every line terminated with one character "line-feed".
In your script a quick fix is to filter the input stream with "tr" to remove the unwanted carriage-return characters.
#! /bin/bash
clear
rdCount=0;
cat ps5.txt | tr -d '\r' | while read myline
do
if [ -z "${myline}" ]; then
echo "line is empty"
else
echo $myline
let rdCount=$rdCount+1
fi
done
echo "COUNT=$rdCount"
Not sure why you are posting the uncorrected script following your previous thread.