?
Solved

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

Posted on 2006-03-22
7
Medium Priority
?
219 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);
0
Comment
Question by:ckaspar
  • 4
  • 2
7 Comments
 
LVL 85

Expert Comment

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

0
 

Author Comment

by:ckaspar
ID: 16264487
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
 
LVL 85

Expert Comment

by:ozo
ID: 16264508
#or
use Tie::File;
tie @array, 'Tie::File', "test.txt" or die "test.txt $!";
$_ .= "\r" for @array;
untie @array;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16264515
#!/usr/bin/perl

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

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

Expert Comment

by:ozo
ID: 16264538
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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16264753
$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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16264879

$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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question