Print to file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Print to file

satimissatimis Posts: 21Member
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

Comments

  • XfactorXfactor Posts: 343Member
    : 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

    It is possible to write to a file inside the script. You can also use bash inside of perl but you have to assign it to a perl variable. Something like this will work:

    [code]
    #!/usr/bin/perl

    $user = `whoami`;
    $now = `date +%Y.%m.%d.%R`;
    $file = "/tmp/satimis/comparison_${user}_${now}.txt";

    open(OUT, ">$file") or die "Can't write to $file: $!";
    print OUT "This data will go to the file, I hope
    ";
    close(OUT);
    [/code]

    For the bash commands above, you have to use the backticks around the commands. Then after you open the file, you write to it using the filehandle, which is the word OUT above.
    Hope this helps.

    X
  • JonathanJonathan Posts: 2,914Member
    : : 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
    :
    : It is possible to write to a file inside the script. You can also use bash inside of perl but you have to assign it to a perl variable. Something like this will work:
    :
    : [code]
    : #!/usr/bin/perl
    :
    : $user = `whoami`;
    : $now = `date +%Y.%m.%d.%R`;
    : $file = "/tmp/satimis/comparison_${user}_${now}.txt";
    :
    : open(OUT, ">$file") or die "Can't write to $file: $!";
    : print OUT "This data will go to the file, I hope
    ";
    : close(OUT);
    : [/code]
    :
    : For the bash commands above, you have to use the backticks around the commands. Then after you open the file, you write to it using the filehandle, which is the word OUT above.
    : Hope this helps.
    :
    If you want all output to go to a file, you can always use:-

    open STDOUT, ">$file";

    Then just doing:-

    print "stuff";

    Prints stuff to the file. Of course, that means you can't write anything else to the console.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • WeirdofreakWeirdofreak Posts: 439Member
    : If you want all output to go to a file, you can always use:-
    :
    : open STDOUT, ">$file";
    :
    : Then just doing:-
    :
    : print "stuff";
    :
    : Prints stuff to the file. Of course, that means you can't write anything else to the console.
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    Would it not be better (and I'd guess faster, but could be wrong) to do a [grey]select FILEHANDLE[/grey], so that if you do need to print to the Terminal you can use [grey]print STDOUT[/grey]? You could also open a new handle with filename "-" I expect - or does that go to STDIN/OUT rather than the console?
  • JonathanJonathan Posts: 2,914Member
    : : If you want all output to go to a file, you can always use:-
    : :
    : : open STDOUT, ">$file";
    : :
    : : Then just doing:-
    : :
    : : print "stuff";
    : :
    : : Prints stuff to the file. Of course, that means you can't write anything else to the console.
    : :
    : : Jonathan
    : :
    : : ###
    : : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    : Would it not be better (and I'd guess faster, but could be wrong) to
    : do a [grey]select FILEHANDLE[/grey], so that if you do need to print
    : to the Terminal you can use [grey]print STDOUT[/grey]?
    Well, it means you have an extra file handle floating around, but that's not *really* a big deal. So yeah, that's another way to do it and if you still might want to print stuff to the console later that works too. There's more than one way to do it. :-)

    : You could also open a new handle with filename "-" I expect - or does
    : that go to STDIN/OUT rather than the console?
    :
    I think "-" in open is for doing IPC stuff with your own children (well, the children of your process... ;-)). If you do something like:-

    $pid = open WRITE_TO_FORK "|-";

    Then that's equivalent to forking and getting a file handle to the forked process' STDIN so you can write stuff to it. You treat $pid as you'd treat the return value of fork().

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • satimissatimis Posts: 21Member
    Hi Jonathan,

    Tks for your advice.

    - snip -
    : : You could also open a new handle with filename "-" I expect - or does
    : : that go to STDIN/OUT rather than the console?
    : :
    : I think "-" in open is for doing IPC stuff with your own children (well, the children of your process... ;-)). If you do something like:-
    :
    : $pid = open WRITE_TO_FORK "|-";
    :
    : Then that's equivalent to forking and getting a file handle to the forked process' STDIN so you can write stuff to it. You treat $pid as you'd treat the return value of fork().

    I solved my problem with following lines

    Script:-
    ......
    ......
    open FILE, ">$file" or die "Can't open $file for output: $!
    ";
    ......
    ......
    ......
    print "
    ";
    open FILE, "<$file" or die "could not open file: $!
    ";;
    while (<FILE> ){
    print;
    }
    close FILE;
    print "
    ";
    print "Filename=$file

    ";

    - END -

    a file created with its content printed on console simultaneously.

    I expect to learn your way. Please advise in more detail how to change those lines.

    TIA

    B.R.
    satimis
  • JonathanJonathan Posts: 2,914Member
    : Hi Jonathan,
    :
    : Tks for your advice.
    :
    : - snip -
    : : : You could also open a new handle with filename "-" I expect - or does
    : : : that go to STDIN/OUT rather than the console?
    : : :
    : : I think "-" in open is for doing IPC stuff with your own children (well, the children of your process... ;-)). If you do something like:-
    : :
    : : $pid = open WRITE_TO_FORK "|-";
    : :
    : : Then that's equivalent to forking and getting a file handle to the forked process' STDIN so you can write stuff to it. You treat $pid as you'd treat the return value of fork().
    :
    : I solved my problem with following lines
    :
    : Script:-
    : ......
    : ......
    : open FILE, ">$file" or die "Can't open $file for output: $!
    ";
    : ......
    : ......
    : ......
    : print "
    ";
    : open FILE, "<$file" or die "could not open file: $!
    ";;
    : while (<FILE> ){
    : print;
    : }
    : close FILE;
    : print "
    ";
    : print "Filename=$file

    ";
    :
    : - END -
    :
    : a file created with its content printed on console simultaneously.
    :
    : I expect to learn your way. Please advise in more detail how to change those lines.
    :
    : TIA
    :
    You can always define a sub called print2 (untested, and I've been doing numerical analysis all evening, nothing seems quite the same now)...

    [code]sub print2 {
    print @_;
    print FILE @_;
    }[/code]

    Then you can do:-

    open FILE, ">$file" or die "Can't open $file for output: $!
    ";

    Where you'd have put print before, you put:-

    print2 "stuff";

    And it prints to the console and the file. Then at the end you just:-

    close FILE;

    And you're done.

    That's one way to do it that's not too evil. It's still not too nice either though.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • satimissatimis Posts: 21Member
    Hi Jonathan,

    Tks for your advice.

    - snip -
    : You can always define a sub called print2 (untested, and I've been doing numerical analysis all evening, nothing seems quite the same now)...
    :
    : [code]sub print2 {
    : print @_;
    : print FILE @_;
    : }[/code]
    :
    : Then you can do:-
    :
    : open FILE, ">$file" or die "Can't open $file for output: $!
    ";
    :
    : Where you'd have put print before, you put:-
    :
    : print2 "stuff";
    :
    : And it prints to the console and the file. Then at the end you just:-
    :
    : close FILE;

    - snip -

    Test performed but failed.
    "Print to file" OK
    but
    "Print on Console" failed

    Printout on Console:-
    $ perl compare_s2_02.pl NewCompare doc_a.txt doc_b.txt


    REMARK :
    OLINENO=Old Line No. NLINENO=New Line No.
    OWORDNO=Old Word No. NWORDNO=New Word No.

    File: NewCompare_satimis_2004.11.19.01:03.txt
    Filehandle FILE opened only for input at compare_s2_pravin02test.pl line 73.
    stuff
    - END -

    Script :-
    #!/opt/bin/perl -w
    use strict;
    use warnings;
    use POSIX qw(strftime);
    use Term::ANSIColor qw(:constants);

    print "
    ";
    print BOLD WHITE ON_MAGENTA "REMARK :", RESET "
    ";
    print RED "OLINENO=Old Line No. NLINENO=New Line No.", RESET "
    ";
    print RED "OWORDNO=Old Word No. NWORDNO=New Word No.", RESET "

    ";

    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
    ";

    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=;
    close(INFO1);

    open FILE, ">$file" or die "Can't open $file for output: $!
    ";

    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++;
    }
    sub print2 {
    print @_;
    print FILE @_;
    }
    open FILE, "<$file" or die "Can't open $file for output: $!
    ";
    print2 "stuff";
    close FILE;
    print "
    ";
    - END of SCRIPT -

    Any suggestion to fix the problem? TIA

    B.R.
    satimis
  • JonathanJonathan Posts: 2,914Member
    : Filehandle FILE opened only for input at compare_s2_pravin02test.pl
    : line 73.
    Well, there's the problem.

    : stuff
    Hmmm...I gave you an example, not something to directly copy and paste into your script.

    : #!/opt/bin/perl -w
    : use strict;
    : use warnings;
    : use POSIX qw(strftime);
    : use Term::ANSIColor qw(:constants);
    :
    : print "
    ";
    : print BOLD WHITE ON_MAGENTA "REMARK :", RESET "
    ";
    : print RED "OLINENO=Old Line No. NLINENO=New Line No.", RESET "
    ";
    : print RED "OWORDNO=Old Word No. NWORDNO=New Word No.", RESET "

    ";
    :
    : 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
    ";
    :
    : 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=;
    : close(INFO1);
    :
    : open FILE, ">$file" or die "Can't open $file for output: $!
    ";
    :
    : 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;
    Aha. You are writing to the file using something other than "print". So what I suggested wouldn't work.

    : Any suggestion to fix the problem? TIA
    :
    You probably may as well do it the way you were before (write to file, then read back in and display the file) - I thought you would just be using the "print" statement to write stuff to the file. I don't use formats so completely forgot about that. Sorry.

    I guess the other option is to define the format for STDOUT too...then do:-

    write FILE;
    write STDOUT;

    But that could be messy.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.