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 and shell scripting languages 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 02:24 PM
how do you get input of a file to calculations nadman123 Shell Programming and Scripting 6 04-11-2008 04:14 AM
redirect input from file? bjornrud UNIX for Dummies Questions & Answers 1 03-09-2008 02:37 AM
Giving input to a c++ file tonyaim83 Shell Programming and Scripting 2 10-23-2007 02:10 AM
how to get input from file ajaya Shell Programming and Scripting 1 04-05-2006 03:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-24-2005
noelcantona noelcantona is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 6
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-
  #2 (permalink)  
Old 10-24-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 05:49 AM..
  #3 (permalink)  
Old 10-24-2005
noelcantona noelcantona is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 6
Thanks blowtorch.

It works like charm.

cheers,
-NoeL-
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0