473,387 Members | 1,465 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.

newline problem when writing to a file

1
Hi all,
I am new to perl and I have written a smal script to grab data from one file, and put them into another file. The problem is new lines, which are printing nice under a linux environment, but it is all messed up if I open it with notepad. I am running Perl 5 under cygwin. Heres the script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open (READ, "opt.txt") || die "Can't find opt.txt\n";
  4.  
  5. $x=0;
  6.  
  7. while ($info = <READ>) {
  8. chomp $info;
  9. @data = split (/\t/, $info);
  10. push @cells, [@data];
  11. $x++;
  12. }
  13.  
  14. close READ || die "Couldn't close opt.txt";
  15. open (WRITE, ">rel.txt") || die "Can't find rel.txt\n";
  16.  
  17. $y=0;
  18.  
  19. print WRITE "#DoNotEditThisLine: UndoCommandFile off_files model_n_m_z endfile=/tmp/6222\n\n";
  20.  
  21. foreach (@cells){
  22. print WRITE "cr balla=1,blablabla=$cells[$y][0],foobar=$cells[$y][0]_$cells[$y][1]\n";
  23. print WRITE "bleh=10175,foounbar=$cells[$y][1]\n\n";
  24.  
  25.  
  26. $y++;
  27. }
  28.  
  29. close WRITE || die "Couldn't close rel.txt";
  30. exit (0);
  31.  
Now if I cat the output in cygwin, the newlines are working. If I open it with notepad, one new line is working which is after print WRITE "bleh=10175,foounbar=$cells[$y][1]\n\n"; (please note not both of newlines are working).

Any ideas? I need those in that specific format
Feb 1 '10 #1
1 5891
chaarmann
785 Expert 512MB
Your problem is that different operating systems (OS) use different characters to make a line break. Therefore "\n" will be converted to one or 2 characters, depending on the OS where the program runs.

UNIX-like Linux, FreeBSD und Solaris use a single LineFeed (LF, ASCII-Code 10).
Mac OS up to Version 9 uses a sinle Carriage Return (CR, ASCII-Code 13).
MS-DOS and Windows use 2 characters to delimit a line: a CR followed by a LF.

The problem is that you have created the text under Unix-environment, therefore you have a single LF. But Notepad runs under Windows and expects CR-LF. Notepad doesn't have the feature to autodetect the correct format, but other editors do: UltraEdit, WinVi, jEdit, ... and, if you still want to stay with Microsoft:
Wordpad!
That means: just use Wordpad instead of Notepad.
Or convert the lf to cr-lf in your text by using Cygwin command unix2dos, before opening your file in Notepad
Feb 1 '10 #2

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

Similar topics

2
by: xegfault | last post by:
Here is a simple script: #!/usr/bin/python print "No newline, please.", # End of script When the program runs, python is still printing a newline when the program exits. How does one keep...
0
by: JC | last post by:
I am using Mysql version: 4.0.18-max-log and trying to populate a database from a text file using source option from inside the mysql environment. The population run OK but when creating the...
3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
2
by: Fred Dag | last post by:
Hi, When writing using both these methods ( StreamWriter.Write("\n") vs WriteLine() ) to a file and open in Notepad I see a special character instead of newlines with Write("\n") vs a newline...
9
by: tubby | last post by:
Silly question, but here goes... what's a good way to determine when a file is an Open Office document? I could look at the file extension, but it seems there would be a better way. VI shows this...
1
by: Jack | last post by:
Hi there MYSQL is loading a non printable character at the end of each row, clearly its a newline or ENTER character.. I have tried dos2unix.exe, a perl program that converts dos files to unix,...
4
by: Jim | last post by:
Hi There, I'm trying to read a file character by character. When I write the file out, there is one extra character which shows on the screen as a solid circle with a small question mark in the...
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.