Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I print unix format files from Windows 1

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
Hi,

I have to create unix format files from windows environment.
So I read in DOS files, parse the data and do things like

print OUTFILE "new data\n";

I'm pretty sure I can use another string escape sequence and
I tried \f and \r, but these did not give me what I needed...

Any suggestions?

AD AUGUSTA PER ANGUSTA

Thierry
 
You don't specify what problem you're having, but I'm guessing it's the line endings. DOS files use \r\n as a line terminator, *nix uses just \n. If you're getting your files from DOS to *nix via ftp, doing the transfer in ascii mode (not binary) should handle the conversion.

You may have a program called dtox or dos2unix which will convert DOS files to *nix format.

Or, you could try s|\r\n|\n| in Perl to convert them.
 
Perl knows which line endings go with which OS so if, in Windows you open a unix file and chomp each line then print each line with "\n", then perl will know to put "\r\n" 'cos you're on Windows. It works the other way round as well.

If you just want to read a *nix file in Windows then use wordpad which is clever enough to realise what you are doing. I suspect also (though I've not tried it) that if you open a *nix file with wordpad and save it under a different file name in windows the conversion will be done for you.
 
I've solved my problem. Just to recap...

I'm in widows environment and I want to create unix format files: this may seem strange but I'm creating input files for a program that was ported from unix to windows, but input files still need to be in uniz format. As greagey is mentions, Perl automatically converts \n to CR+LF in windows. I tried the substitue solution from mikevh, but this did not work.

Finally after spending many hours on internet, I found a nice sample here:
The solution seems to be to write as binary data, so the following solution works fine for me:
Code:
#!/usr/bin/perl -w

# Declare modules
  use strict;
  use Fcntl qw{O_WRONLY O_CREAT};

# Define the file to write to
  my $filename = "xx.txt";

# Define a string and add LF instead of \n
  my $string = "Hello World" . chr(0x0A);

# Open file and write binary data
  sysopen( FH, $filename, O_WRONLY | O_CREAT ) or die "sysopen: file '$filename': $!\n";
  binmode( FH);
  syswrite( FH, $string) or die "syswrite: file '$filename': $!\n";
  close FH;

Hope this helps somebody someday.

AD AUGUSTA PER ANGUSTA

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top