The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SED + Regex + SQL Input file primal Shell Programming and Scripting 10 04-14-2008 10:24 AM
how do you get input of a file to calculations nadman123 Shell Programming and Scripting 6 04-11-2008 12:14 AM
redirect input from file? bjornrud UNIX for Dummies Questions & Answers 1 03-08-2008 10:37 PM
Giving input to a c++ file tonyaim83 Shell Programming and Scripting 2 10-22-2007 10:10 PM
how to get input from file ajaya Shell Programming and Scripting 1 04-05-2006 11:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-24-2005
Registered User
 

Join Date: Oct 2005
Posts: 6
Stumble this Post!
Problem with ^M in input file

Greetings to all,

I have this problem comparing two file and extract the output from it.

"input.txt" file(Extracted from excel):
ABC
ABB
ABA

"compare.txt" file(Extracted from excel):
ABA 1
ABB 2
ABC 3
ABD 4
ABE 5
ABF 6
ABG 7

I would like to get output of
ABC 3
ABB 2
ABA 1

This is my script:
-----------------
while read record
do
grep "$record" compare.txt
done < input.txt > output.txt

The result of `wc -l output.txt` is zero.

I later `echo "try" > input.txt
and when I `vi input.txt` I found this:

ABC^M
ABB^M
ABA^M
try

Question:
Why is the ^M appearing and how to get rid of it?

I tried removing the ^M manually and my output is all NICE.

Please advice.

Thank you.

cheers,
-NoeL-
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-24-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Stumble this Post!
The ^M appears in files that come over from Windows or DOS. This is because the line ending character in Unix is "\n", while in Windows and DOS, you have "\r\n". The "\n" from these is shown as the ^M.

For a nice explaination go here.

To get rid of these characters, you can use the dos2unix (Solaris) or dos2ux (HP) commands. Or these little perl statements:
Code:
# neither of the following perl statements have been tested by me
perl -pe 's/\015\012/\n/g' ##- dos to unix -- just pipe through it
perl -pe 's/\n/\015\012/g' ##- unix to dos -- just pipe through it
Or do what I do:
Code:
strings file_to_convert > converted_file; mv converted_file file_to_convert

Last edited by blowtorch; 10-24-2005 at 01:49 AM.
Reply With Quote
  #3 (permalink)  
Old 10-24-2005
Registered User
 

Join Date: Oct 2005
Posts: 6
Stumble this Post!
Thanks blowtorch.

It works like charm.

cheers,
-NoeL-
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0