By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Problems writing stdout to a file

P: 8
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Oct 2 '07 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Well, the reason is, you copied from a Windows machine to a Unix based machine. The newline in Windows is a carriage return/line feed, but in Unix it is only a single line feed, no carriage return. Thus, when you view a file on Unix that has been on a Windows system, you will see a ^M at the end of every line. To avoid this, simply do this:

Expand|Select|Wrap|Line Numbers
  1. chomp($_);
  2.  
before the print statement. That will remove the ^M from the end of the line. You should also use chomp on variables after accepting input from the user. The reason? What is the last thing the user does to submit their answer? Hit enter, causing a carriage return/line feed to be entered into the variable.

Regards,

Jeff
Oct 2 '07 #2

mehj123
P: 55
Hi.. if you just want to remove the ^M characters from the output file, you can use the command
Expand|Select|Wrap|Line Numbers
  1.  dos2unix filename filename
.
this command will remove the ^M s.... actually this command changes the file from the dos format to unix format..

filename can be the same name or different name.. giving same name over writes the existing file..

I dont know if this is any help as this is not related to scripting.. once you get the output file you can use this command to remove ^M..
Oct 3 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi.. if you just want to remove the ^M characters from the output file, you can use the command
Expand|Select|Wrap|Line Numbers
  1.  dos2unix filename filename
.
this command will remove the ^M s.... actually this command changes the file from the dos format to unix format..

filename can be the same name or different name.. giving same name over writes the existing file..

I dont know if this is any help as this is not related to scripting.. once you get the output file you can use this command to remove ^M..
You are correct that dos2unix does achieve this, but the OP would not have to issue an extra system command if they simply used the chomp() command in their code which does the same thing as dos2unix, but on each line before it is written to the file.

Regards,

Jeff
Oct 3 '07 #4

mehj123
P: 55
Ya Jeff.. you r correct :)
Oct 3 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.