Unix-problem of New line character

Hi All,

"Please read the below information carefully."

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.

please help me to solve the above problm

Thanks

  1. Please post the output of cat -A ps5.txt
  2. Or open ps5.txt in vi and check if you can see ^M at the end of each line. If so, do this in vi :%s/<ctrl+v+m>//g

Hi Balajesuri,

See the below output

[user01@rk03 exercise]$ cat -A ps5.txt
hkj^M$
h^M$
h^M$
^M$
^M$
h^M$

As I suspected, try what I mentioned in point #2 of post #2.

Alternatively, you can also use dos2unix or the following too:
sed -i 's/<ctrl+v+m>//g' inputfile

Hi balajesuri,

tried below code

:%s/<ctrl+v+m>//g

getting error "patteren not fount :<ctrl+v+m>/

can you please tell me in details

Do not type <ctrl+v+m> literally. I meant, press "ctrl, v, m" keys together so that you get ^M.

dos2unix not working?

--ahamed

hi balajesuri...

not getting exactly

if i am in vi editor then hoe can i write the code :%s/<ctrl+v+m>//g and press "ctrl, v, m" keys together .... at a time.

please help me to clear this?

2 ) and lastly do we need to save this file using :wq ??

---------- Post updated at 05:56 AM ---------- Previous update was at 05:53 AM ----------

hey got the point of ctrl M and ctrl v thanks...

---------- Post updated at 05:59 AM ---------- Previous update was at 05:56 AM ----------

I have tried the above code.... but i am getting error

error:- patteren not found: ^M :wall:

---------- Post updated at 06:11 AM ---------- Previous update was at 05:59 AM ----------

hey thanks a lot its working :slight_smile:

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

Hi,

i am able to run the above command successfully.

sed -i 'sed -i 's/^M//g' ps6.txt

--> if i type this command manually then its working.

but if i copy paste the above command in any other directory the its not working

and i want write the same code in shellscript like below

#!/bin/bash
clear
cd /home/user01/trial1
sed -i 's/^M//g' $inPutFile
rdCount=0;
while read myline

then the sed command is not working here. can you please give me the solution because each time i cant type this command..

Thanks

---------- Post updated at 06:38 AM ---------- Previous update was at 06:15 AM ----------

Its working thanks

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.