How would I add carage returns to a text file file in UNIX Part 2

I got this example yesterday which works on 2 different files. Does anyone know how to manipulate just one file?
I need the input file and output file to be the same.
e.g. open(IN,"<test.txt");
      open(OUT,">test.txt");
This will create a blank file. But I need to open test.txt and insert the carage returns on test.txt. The code below make a duplicate of test.txt to out.txt.

open(IN,"<test.txt");
open(OUT,">out.txt");
while ( <IN> )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(IN);
close(OUT);
ckasparAsked:
Who is Participating?
 
ozoCommented:
#!/usr/bin/perl

my $file = "/home/scr/file";

{
  local @ARGV=($file);
  local $^I="";
  local $\="\r\n";
  chomp,print while <>;
}
0
 
ozoCommented:
{
local @ARGV=("test.txt",);
local $^I="";
local $\="\r\n";
chomp,print while <>;
}

0
 
ckasparAuthor Commented:
I tried that and got this error -> Can't open $file: No such file or directory?
Any ideas?

#!/usr/bin/perl

my $file = "/home/scr/file";

{
  local @ARGV=qw($file);
  local $^I="";
  local $\="\r\n";
  chomp,print while <>;
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ozoCommented:
#or
use Tie::File;
tie @array, 'Tie::File', "test.txt" or die "test.txt $!";
$_ .= "\r" for @array;
untie @array;
0
 
ozoCommented:
qw($file) is an array contaning the literal string '$file'

       qw/STRING/
               Evaluates to a list of the words extracted out of STRING, using
               embedded whitespace as the word delimiters.  It can be under-
               stood as being roughly equivalent to:

                   split(' ', q/STRING/);

               the differences being that it generates a real list at compile
               time, and in scalar context it returns the last element in the
               list.  So this expression:

                   qw(foo bar baz)

               is semantically equivalent to the list:

                   'foo', 'bar', 'baz'

               Some frequently seen examples:

                   use POSIX qw( setlocale localeconv )
                   @EXPORT = qw( foo bar baz );

               A common mistake is to try to separate the words with comma or
               to put comments into a multi-line "qw"-string.  For this rea-
               son, the "use warnings" pragma and the -w switch (that is, the
               $^W variable) produces warnings if the STRING contains the ","
               or the "#" character.
0
 
Kim RyanIT ConsultantCommented:
$in_file = 'data.txt';
$out_file = 'tmp.txt';

open(IN,"<$in_file");
open(OUT,">$out_file");
while ( <IN> )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(IN);
close(OUT);
rename($out_file,$in_file); # replace original file with modified version
0
 
Kim RyanIT ConsultantCommented:

$file = 'test.txt';
open(IN,"<$file");
@lines = <IN>; # get all lines in file
close(IN);
open(OUT,">$file");
foreach ( @lines )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(OUT);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.