Click to See Complete Forum and Search --> : Perl - how to print to file
satimis
11-12-2004, 11:52 PM
Hi folks,
Instead of using following command printing output to a file;
$ perl AAA.pl > /pathto/name_of_file
is it possible adding "print file function" to the script
1) If YES
Please advise how to make it.
2) Can I add following bash syntax to the script
user=$(whoami);
now=$(date +%Y.%m.%d.%R);
File="/tmp/satimis/comparison_${user}_${now}.txt";
so that the printout file will carry name of creator, date and time, etc.
If possible please shed me some light to re-edit the script.
TIA
B.R.
satimis
Uranus
11-13-2004, 06:07 AM
I'm not sure exactly how this works anymore - but it is definitely (easily) possible. Just use "open".
open ( OUTPUT, >$file ) ;
then to write to it:
print OUTPUT ("What you want to print!") ;
You should have a look at this page:
http://docs.rinet.ru/P7/ch6.htm
I'm not sure how you'd go about executing non-perl commands. I have to continue working on that guide some time soon.
Sam
satimis
11-14-2004, 11:48 PM
Hi Uranus,
Tks for your advice.
What I expect to do is on executing a perl command it displays output on terminal and simultaneously printing to a file.
$ perl AAA.pl >/path/to/AAA.txt
will do the job, displaying the result on terminal and printing a file on /path/to/AAA.txt
But I want it to be written on the script.
B.R.
satimis
squeegy
11-15-2004, 01:26 AM
Use friendly old back ticks.
$user = `whoami`;
$date = `date +%Y.%m.%d.%R`;
open(OUTPUTFILE,">/tmp/satimis/comparison_$user_$date.txt");
print OUTPUTFILE "whatever you'd like to be in the file";
print "whatever you'd like to be displayed to the screen";
satimis
11-15-2004, 04:17 AM
Hi squeegy,
Tks for your advice.
Test performed with /tmp/satimis/comparison_2004.11.15.15:55.txt created but username missed.
$ cat compare_s2_02.pl#!/usr/bin/perl -w
#use strict;
use warnings;
$user=`whoami`;
$now=`date +%Y.%m.%d.%R`;
$file="/tmp/satimis/comparison_$user_$now.txt";
open STDOUT,">$file";
print STDOUT "$line\n";
#print "/tmp/satimis/comparison_*";
format STDOUT_TOP=
ORGINAL MISTAKE LINENO WORDNO
.
format STDOUT_MID=
@<<<<<<<<<< @<<<<<<<<<< @## @##
$org, $mis, $lno1, $wno
.
$orgfile='doc_a.txt';
open(INFO,$orgfile)|| die 'cannot open $orgfile';
@basedoc=<INFO>;
close(INFO);
$typfile='doc_b.txt';
open(INFO1,$typfile) || die 'cannot open $typfile';
@typedoc=<INFO1>;
close(INFO1);
$~="STDOUT_TOP";
write();
$lno=0;
$wno=0;
foreach $i (@typedoc)
{
$incr=0;
@typedoc1=split(/ /,$i);
@basedoc1=split(/ /,$basedoc[$lno]);
foreach $k (@typedoc1)
{
$var=$basedoc1[$incr];
if ( $var ne $k )
{
$org=$var;
$mis=$k;
$wno=$incr+1;
$lno1=$lno+1;
$~="STDOUT_MID";
write();
}
$incr++;
}
$lno++;
}The file created can't be printed on terminal. I tried many variasion
print "$file";
print "$file\n";
print "/tmp/satimis/comparison_*";
print $file;
etc.
None can work printing the content of the file just created on terminal. Instead it re-edited the content of /tmp/satimis/comparison_2004.11.15.15:55.txt .
Printout:$ perl compare_s2_pravin02test.pl doc_a.txt doc_b.txt
Name "main::user_" used only once: possible typo at compare_s2_pravin02test.pl line 14.
Name "main::line" used only once: possible typo at compare_s2_pravin02test.pl line 17.
Name "main::user" used only once: possible typo at compare_s2_pravin02test.pl line 12.
Use of uninitialized value in concatenation (.) or string at compare_s2_pravin02test.pl line 14.
Use of uninitialized value in concatenation (.) or string at compare_s2_pravin02test.pl line 17.
line 12
$user=`whoami`;
line 14
$file="/tmp/satimis/comparison_$user_$now.txt";
line 17
print STDOUT "$line\n";
"use strict" must be commented out otherwise there were many complaint such as;
Global symbol "$user" requires explicit package name at compare_s2_02.pl line 12.
Global symbol "$now" requires explicit package name at compare_s2_02.pl line 13.
Global symbol "$file" requires explicit package name at compare_s2_02.pl line 14.
etc.
What is "explicit package"? Please advise. TIA
B.R.
satimis
scinerd
11-15-2004, 09:47 AM
A few thinkings I noticed First the What is "explicit package"? question. When you use strict you must declare all your varibables before you use them. For example with out strict you can say "$user = `whoami`;" but with strict you need to declaire it with so you need to put "my $user = `whoami`;" This declaires it at a local variable and you can use our instead of my to make it global.
there a few problems in this
$user=`whoami`;
$now=`date +%Y.%m.%d.%R`;
$file="/tmp/satimis/comparison_$user_$now.txt";
here I have rewritten it
chomp ($user=`whoami`);
chomp ($now=`date +%Y.%m.%d.%R`);
$file="/tmp/satimis/comparison_$user\_$now.txt";
you need to add the chomp because when a command returns it includes a newline char. The chomp will remove it so your $file doesn't have newlines in the middle. Also the "_" after $user was making $user a different variable "$user_" so it need to be escaped with a \.
Printing a file in perl you can't just do print "$file"; the closet thing you could do is run a
system "cat $file";
to do it through perl you need to open the file and print it like so
my $file = "testfile";
open F , "<testfile" or die "could not open file: $!\n";;
while (<F>){
print;
}
close F;
Notice that I close my filehandle when I'm done. This will happen when the program ends but it's good to get in the habbit of closeing you files cleanly. It's also good to print an error if your file can't be opened.
With the while the current line is put into $_ and when I say print; it prints that variable. I don't need to put \n anywhere since each line in the file already as a new line char
hope this helps.
Uranus
11-15-2004, 10:50 AM
Originally posted by satimis
The file created can't be printed on terminal. I tried many variasion
print "$file";
print "$file\n";
print "/tmp/satimis/comparison_*";
print $file;
etc.
None can work printing the content of the file just created on terminal. Instead it re-edited the content of /tmp/satimis/comparison_2004.11.15.15:55.txt .
I don't know what kind of terminal you're using - but I know of no terminal that uses "print" to output a file. Use "cat" or "less" or open it in a text editor like "vim".
And may I kindly advice not to name the output file STDOUT - this is confusing, when reading the code without executing I would not know whether it prints to the console or to a file. Instead name it something like "OUTPUT".
Sam
satimis
11-15-2004, 11:49 AM
Hi scinerd,
Tks for your advice. After rearranging the perl script now it works
$ cat compare_s2_02.pl#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX qw(strftime);
my $user=`whoami`;
chomp $user;
my $now = strftime "%Y.%m.%d.%R", localtime;
my $file="comparison_${user}_${now}.txt";
my ($org, $mis, $lno1, $wno);
format FILE_TOP=
ORGINAL MISTAKE LINENO WORDNO
.
format FILE=
@<<<<<<<<<< @<<<<<<<<<< @## @##
$org, $mis, $lno1, $wno
.
my $orgfile='doc_a.txt';
open(INFO,$orgfile) || die "Cannot open $orgfile: $!";
my @basedoc=<INFO>;
close(INFO);
my $typfile='doc_b.txt';
open(INFO1,$typfile) || die "Cannot open $typfile: $!";
my @typedoc=<INFO1>;
close(INFO1);
open FILE, ">$file" or die "Can't open $file for output: $!\n";
my $lno=0;
$wno=0;
foreach my $i (@typedoc) {
my $incr=0;
my @typedoc1=split(/ /,$i);
my @basedoc1=split(/ /,$basedoc[$lno]);
foreach my $k (@typedoc1){
my $var=$basedoc1[$incr];
if ( $var ne $k ){
$org=$var;
$mis=$k;
$wno=$incr+1;
$lno1=$lno+1;
#$~="FILE_MID";
write FILE;
}
$incr++;
}
$lno++;
}
print "\n";
open FILE, "<$file" or die "could not open file: $!\n";;
while (<FILE> ){
print;
}
close FILE;
print "\n";
print "Filename=$file\n\n";
Printout on terminal:>$ perl compare_s2_02.pl doc_a.txt doc_b.txt
ORGINAL MISTAKE LINENO WORDNO
this thes 1 2
supported suppurted 1 9
SANE SeNE 1 11
Filename=comparison_satimis_2004.11.15.23:14.txt
$>If I need to make the filename variable to be input on running the script. Kindly advise how to make it.
TIA
B.R.
satimis
scinerd
11-15-2004, 02:34 PM
#!/opt/bin/perl
use strict;
chomp (my $file = @ARGV[0]);
print "$file\n";
this is just a little script to show how to pass aruguments. If you run it at "script.pl filename" you will get the out put "filename" All the arguments given at run time are put in @ARGV and can be read from there like a normal array.
you can also do something like this:
#!/opt/bin/perl
use strict;
while (<>){
print ;
}
the <> is short for stdin so if you cat a file and pipe to this script it will print out each line. it's a nice little trick that comes in handy for some scripts.
If you are going to continue to do work in perl I would suggest you pick up "programming perl" which is a O'reilly book. It covers all your questions and more.
squeegy
11-15-2004, 03:04 PM
Originally posted by scinerd If you are going to continue to do work in perl I would suggest you pick up "programming perl" which is a O'reilly book. It covers all your questions and more.
I agree with this statement 100% I own both Learning Perl (Llama), Programming Perl (Camel), and Perl Cookbook (Ram). They were all extremely useful additions to my collection of misc programming books.
satimis
11-17-2004, 06:28 AM
Hi scinerd
Tks for your advice. .....
this is just a little script to show how to pass aruguments. If you run it at "script.pl filename" you will get the out put "filename" All the arguments given at run time are put in @ARGV and can be read from there like a normal array.....
What I expect to do is to make "comparison" as variable
.......
my $user=`whoami`;
chomp $user;
my $now = strftime "%Y.%m.%d.%R", localtime;
my $file="comparison_${user}_${now}.txt";
.....The name of file is keyed in together with running the script at start. Any suggestion?
Besides, I have been trying to add printing feature to the heading of
ORGINAL MISTAKE LINENO WORDNO
in the script without success. I don't know where should be the correct place to add
print BOLD WHITE ON_MAGENTA "text to be printed", RESET "\n";
could you please shed me some suggestions.
I already added#!/usr/bin/perl -w
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
......
TIA
B.R.
satimis
scinerd
11-17-2004, 10:24 AM
not sure what your asking for the comparision. You can pass in as many things as you want with the @ARGV array. To copre to variables you can do
if ( $file eq $file) {
#if equal do something
}
The eq checks to variables with text. The == will compare two variables with numbers. watch out for end of lines at the end of text since they will not be equal if one has it and one dioesn't.
The formating stuff I'm not sure since I really don't do it.
satimis
11-17-2004, 12:47 PM
Hi scinerd,
not sure what your asking for the comparision. ..."comparison" is the name of file which I want to make it variable. That is "name of file" to be entered in running the command;
$ perl script.pl <filename>
The date and time are to be added automatically as in the running script
B.R.
satimis
scinerd
11-17-2004, 07:21 PM
I think this is what your looking for
#!/opt/bin/perl
use strict;
use POSIX qw(strftime);
chomp (my $file = @ARGV[0]);
chomp (my $user=`whoami`);
my $now = strftime "%Y.%m.%d.%R", localtime;
my $file="$ARGV[0]_${user}_${now}.txt";
print "$file \n";
try doing the rest on your own and if something doesn't work post the code and I/someone can help debug it.
satimis
11-18-2004, 12:45 AM
I think this is what your looking for.......
Hi scinerd and folks,
Tks for your advice. I have the Perl script re-edited as follow;
$ cat compare_s2_02.pl
#!/opt/bin/perl -w
use strict;
use warnings;
use POSIX qw(strftime);
use Term::ANSIColor qw(:constants);
print "\n";
print BOLD WHITE ON_MAGENTA "REMARK :", RESET "\n";
print RED "OLINENO=Old Line No. NLINENO=New Line No.", RESET "\n";
print RED "OWORDNO=Old Word No. NWORDNO=New Word No.", RESET "\n\n";
chomp (my $file=$ARGV[0]);
chomp (my $user=`whoami`);
my $now = strftime "%Y.%m.%d.%R", localtime;
$file="$ARGV[0]_${user}_${now}.txt";
my ($org, $mis, $lno1, $wno, $lno2, $wno1);
print "File: $file \n";
format FILE_TOP=
ORGINAL MISTAKE OLINENO OWORDNO NLINENO NWORDNO
.
format FILE=
@<<<<<<<<<< @<<<<<<<<<< @## @## @## @##
$org, $mis, $lno1, $wno, $lno2, $wno1
.
my $orgfile='doc_a.txt';
open(INFO,$orgfile) || die "Cannot open $orgfile: $!";
my @basedoc=<INFO>;
close(INFO);
my $typfile='doc_b.txt';
open(INFO1,$typfile) || die "Cannot open $typfile: $!";
my @typedoc=<INFO1>;
close(INFO1);
open FILE, ">$file" or die "Can't open $file for output: $!\n";
my $lno=0;
$wno=0;
foreach my $i (@typedoc) {
my $incr=0;
my @typedoc1=split(/ /,$i);
my @basedoc1=split(/ /,$basedoc[$lno]);
foreach my $k (@typedoc1){
my $var=$basedoc1[$incr];
if ( $var ne $k ){
$org=$var;
$mis=$k;
$wno=$incr+1;
$lno1=$lno+1;
$wno1=$incr+1;
$lno2=$lno+1;
#$~="FILE_MID";
write FILE;
}
$incr++;
}
$lno++;
}
print "\n";
open FILE, "<$file" or die "could not open file: $!\n";;
while (<FILE> ){
print;
}
close FILE;
print "\n";
It worked nicely.
$ perl compare_s2_02.pl Newcomparison doc_a.txt doc_b.txt
REMARK :
OLINENO=Old Line No. NLINENO=New Line No.
OWORDNO=Old Word No. NWORDNO=New Word No.
File: Newcomparison_satimis_2004.11.18.06:22.txt
ORGINAL MISTAKE OLINENO OWORDNO NLINENO NWORDNO
this thes 1 2 1 2
supported suppurted 1 9 1 9
SANE SeNE 1 11 1 11
I can't add "my" before "$file" in following line.....
$file="$ARGV[0]_${user}_${now}.txt";
.....
otherwise it complained.
The script also worked on multi-line document without problem.
BUT NIGHTMARE CAME;
====================
1) If a word left out in one line
2) If a word left out in one line and words on following line pushed up
Example 1) If a word left out in one line:
$ cat doc_a.txtCheck this link to sea what scannars are supported by SANE
The '>' symbol indicates the file should be open for output. If the file doesn't exist, perl
will create it. This command will fail if you try to create the file in a directory where the
user doesn't have write permission.
And example of printing an array of lines to a file would look like this:
cat doc_b.txtCheck thes link to sea what scannars are suppurted by SeNE
The '>' symbl indicates the file should be open for output. If the file doesn't exist, perl
will create it. This command will fall if you try to create the file in a directary where the
user doesn't have (write) permission.
And examplle of printing an array of lines to a filee would look like this:The word (write) left out
$ perl compare_s2_02.pl Newcomparison doc_a.txt doc_b.txt
REMARK :
OLINENO=Old Line No. NLINENO=New Line No.
OWORDNO=Old Word No. NWORDNO=New Word No.
File: Newcomparison_satimis_2004.11.18.07:05.txt
ORGINAL MISTAKE OLINENO OWORDNO NLINENO NWORDNO
this thes 1 2 1 2
supported suppurted 1 9 1 9
SANE SeNE 1 11 1 11
symbol symbl 3 3 3 3
perl perl 3 18 3 18
fail fall 4 8 4 8
directory directary 4 18 4 18
write permission. 5 4 5 4
example examplle 7 2 7 2
file filee 7 11 7 11
No indication of "word missing"
Example 2) If a word left out in one line and words on following line pushed up:
$ cat doc_b.txtCheck thes link to sea what scannars are suppurted by SeNE
The '>' symbl indicates the file should be open for output. If the file doesn't exist, will
create it. This command will fall if you try to create the file in a directary where the
user doesn't have write permission.
And examplle of printing an array of lines to a filee would look like this:the word "perl" on the 3rd line left out. "will" on following line was pushed up one line.
$ perl compare_s2_02.pl Newcomparison doc_a.txt doc_b.txt
REMARK :
OLINENO=Old Line No. NLINENO=New Line No.
OWORDNO=Old Word No. NWORDNO=New Word No.
File: Newcomparison_satimis_2004.11.18.06:57.txt
ORGINAL MISTAKE OLINENO OWORDNO NLINENO NWORDNO
this thes 1 2 1 2
supported suppurted 1 9 1 9
SANE SeNE 1 11 1 11
symbol symbl 3 3 3 3
perl will 3 18 3 18
will create 4 1 4 1
create it. 4 2 4 2
it. 4 3 4 3
This 4 4 4 4
This command 4 5 4 5
command will 4 6 4 6
will fall 4 7 4 7
fail if 4 8 4 8
if you 4 9 4 9
you try 4 10 4 10
try to 4 11 4 11
to create 4 12 4 12
create the 4 13 4 13
the file 4 14 4 14
file in 4 15 4 15
in a 4 16 4 16
a directary 4 17 4 17
directory where 4 18 4 18
where the 4 19 4 19
the 4 20 4 20
example examplle 7 2 7 2
file filee 7 11 7 11The output was in complete disorder.
Any suggestion?
Furthermore how to make the output on terminal displayed inside a window with background coloured??? TIA
B.R.
satimis