473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems writing stdout to a file

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
4 2224
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
55
Ya Jeff.. you r correct :)
Oct 3 '07 #5

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

Similar topics

7
by: Paul Watson | last post by:
How can I write lines to stdout on a Windows machine without having '\n' expanded to '\r\n'. I need to do this on Python 2.1 and 2.3+. I see the msvcrt.setmode function. Is this my only path?...
7
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout,...
10
by: Nainto | last post by:
Hello, I have posted before about trying to find the status of an FTP uplaod but couldn't get anything to work. After some more searching I found...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
3
by: kimimaro | last post by:
hi below is my save function that is used to placed data from the C program to a text file for future usage. void save() { FILE *save; int i = 0; save=fopen("employeerecord.txt", "a+");
2
by: goodnamesalltaken | last post by:
Hello fellow python users, I've been working on a basic implementation of a privilege separated web server, and I've goto the point of running a basic cgi script. Basically when the execCGI...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
12
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i)...
4
by: pcfreak30 | last post by:
ok, i probably have not been registered here long, but i am no noob. I am very experienced in php, but the php cli is a bit of new territory for me. I am trying to create a simple little script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.