We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

ckaspar
ckaspar asked
on
Medium Priority
242 Views
Last Modified: 2010-03-05
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);
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

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

Author

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 <>;
}
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#or
use Tie::File;
tie @array, 'Tie::File', "test.txt" or die "test.txt $!";
$_ .= "\r" for @array;
untie @array;
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/usr/bin/perl

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

{
  local @ARGV=($file);
  local $^I="";
  local $\="\r\n";
  chomp,print while <>;
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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.
Kim RyanIT Consultant

Commented:
$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
Kim RyanIT Consultant

Commented:

$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);
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.