Unix-problem of New line character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix-problem of New line character
# 1  
Old 01-18-2012
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

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 < ps5.txt
echo "COUNT=$rdCount"


and my ps5.txt contains data-

Code:
a
 
s
 
c


its giving me the correct output i.e


Code:
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


Code:
hkj
h
h
 
 
h


when i have run the above script its giving me the wrong output i.e

Code:
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
# 2  
Old 01-18-2012
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
# 3  
Old 01-18-2012
Hi Balajesuri,

See the below output

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


Last edited by Franklin52; 01-18-2012 at 04:49 AM..
# 4  
Old 01-18-2012
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
# 5  
Old 01-18-2012
Hi balajesuri,

tried below code

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

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


can you please tell me in details
# 6  
Old 01-18-2012
Do not type <ctrl+v+m> literally. I meant, press "ctrl, v, m" keys together so that you get ^M.
# 7  
Old 01-18-2012
dos2unix not working?

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

2. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

3. Shell Programming and Scripting

Unix command to select first few characters and last character of a line

I have a huge file and I want to select first 10 charcters and last 2 characters of everyline and than will filter the unique line. I know, it must be easy bt I am new to unix scripting:) Ex. I have file as below and need to e3kbaird and last 2 characters. and than unique records. ... (3 Replies)
Discussion started by: Sanjeev Yadav
3 Replies

4. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

HOW: Dealing with new line character Wiindows-vs-UNIX

Hi I am getting this similar below lines, by splitting one big file by using the code in my shell script Line is:- 111111111| +.00|12/11/04|12/11/05|n 222222222| +.00|12/11/05|12/11/06|n 333333333| +.00|12/11/06|10/11/07|n Code is:- ... ... ... nawk -F"|" -v v1="${v_pno}"... (7 Replies)
Discussion started by: shekharjchandra
7 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. Shell Programming and Scripting

New line character problem with ksh on Redhat Linux

Guys, I would like to discuss the problem I am seeing with echo “\n” statement in Redhat Linux Enterprise 5.3 version. I have a shell script that was written couple of years back for generic UNIX platforms based on ksh and was tested on Solaris 8,9,10 ; AIX 5.3 and Red Hat Enterprise Linux... (5 Replies)
Discussion started by: rijeshpp
5 Replies

9. Shell Programming and Scripting

Unix character set problem

Hi All, We are getting file into our unix box with multibyte characters. When we tried to view the file the record looks like this Frédéric Actually the data sent to us is Frédéric --> my locale charmap of unix is set to UTF8 only ... but still i am getting this problem. I... (6 Replies)
Discussion started by: sandeeppvk
6 Replies
Login or Register to Ask a Question