Append Output to another file in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append Output to another file in Perl
# 1  
Old 09-02-2008
Append Output to another file in Perl

Hi All,

I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2.
I tried out with the below code but couldn't work.
Can any expert give me some advice?

Code:
open(OUTPUT, 'perl myscript.pl file1 |');
close OUTPUT;
open(OUTPUT, ">> file2");
close OUTPUT;

# 2  
Old 09-02-2008
actually nothing is being written to the required file,

you are opening the process, closing the stream

reopening destination file in append mode and closing the stream

instead
open the process ( with | command )
open the destination file with another file handle
write the contents to using the new file handle

close both the file handles

Please use local file handles
# 3  
Old 09-02-2008
Hi matrixmadhan,

Do you mean the below ?
I can;t seem to get the correct output with the below.
Can you show me an example ?

Code:
open(OUTPUT1, 'perl myscript.pl file1 |');
open(OUTPUT2, ">> file2");
close OUTPUT1;
close OUTPUT2;

# 4  
Old 09-02-2008
You are just opening and closing, not actually writing anything to the file.

By the looks of it, you simply want (in the shell)

Code:
perl myscript.pl file1 >>file2

If you really want to do this in Perl, something like

Code:
open (INPUT, 'perl myscript.pl file1 |') or die "$!\n";
open (OUTPUT, ">>file2") or die "$!\n";
while (<INPUT>) { # read a line of input from myscript.pl
  print OUTPUT;  # print it to the output handle
}
close INPUT;
close OUTPUT;

# 5  
Old 09-03-2008
Hi Era,

I encounter the following error when i use your format.
Can you give some advice ?
Seems to me that the Perl cannot accept this syntax :
open(INPUT, 'perl myscript.pl @arr_y[$num] |');

Error Msg:
Can't open @arr_y[]: No such file or directory at myscript.pl

Code:
$num = 0;

while ($num <= $#arr_y ) {
        open(FH,"< file2") or die $!;
        print "@LISTING[$num] is undercheck now.\n";
                while( <FH> ) {
                chomp;
                if($_ =~ @LISTING[$num] && $_ =~ @SLIST[$num]) {
                        $check_file = "already_appended"
                        };
                close FH;
                print "@arr_y[$num]\n";
                if($check_file ne 'already_appended') {
                        open(INPUT, 'perl myscript.pl @arr_y[$num] |');
                        open(OUTPUT, ">> file2");
                        while (<INPUT>) {       # read a line of input from myscript.pl
                                print OUTPUT;   # print it to the output handle
                        }                       
                        close INPUT;
                        close OUTPUT; 
                        print "@LISTING[$num] will be appended.\n"
                }
                else
                {
                        print "@LISTING[$num] will NOT be appended.\n";
                };
                };
        $num = $num + 1;
};
};


Last edited by Raynon; 09-03-2008 at 12:33 AM..
# 6  
Old 09-03-2008
what you have quoted will not expand.

Instead, write the command to a variable something like
Code:
my $command = "perl scriptname.pl @args | ";

#expand argument list here as though its been executed from command lin
# then open the pipe in the same command

Code:
my $pipe_des;
open($pipe_des, $command) or die "Unable to open pipe command : $command <$!> \n";

# 7  
Old 09-03-2008
Unrelated to that, you are closing the filehandle after reading just the first line, is that intentional? See https://www.unix.com/shell-programmin...ents-perl.html -- are you on the same project as this other guy?

You are appending to file2 just after reading it, and if the close is wrong, you would apparently continue to read from it after appending; how exactly should this work, or rather, what are you trying to accomplish?

You probably mean $arr_y[$num] rather than @arr_y[$num]

Calling out to Perl via the shell from another Perl script is not very elegant, I guess there can be reasons why you want to do it that way, but refactoring myscript.pl so that you can require it from within Perl directly might make some things less awkward.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need solution to compare two file and update and append the output

Hi All, I have two files File1 frame,007C1 server1_Parent frame,007C3 server2_Silver frame,007EE server3_Bronze frame,00855 server4_Parent frame,00856 server4_Parent frame,00858 server5_Parent frame,008FA server6_Silver frame,008FB server6_Silver frame,008FC server6_Silver... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Append to a file repeating output

Hello, i'm trying to force a command to read every second from an interface watch -n1 (command) /dev/x | cat >> output but it continue to overwrite the file, without append the content Thanks and advace for help as usual regards (4 Replies)
Discussion started by: Board27
4 Replies

3. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

4. UNIX for Dummies Questions & Answers

Noob questions.. Append output to a file in different directory

Noob question! I know almost nothing so far, and I'm trying to teach myself from books, on a typical command line without using scripts how would I append output from a sort to a file in a completely different directory? example: If I'm sorting a file in my documents directory but I... (2 Replies)
Discussion started by: Byrang
2 Replies

5. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

6. Shell Programming and Scripting

append an output file with two columns

Hi All, can you help me with this: grep XXX dir/*.txt|wc -l > newfile.txt - this put the results in the newfile.txt, but I want to add another column in the newfile.txt, string 'YYYYY', separated somehow, which corresponds on the grep results? For example grep will grep XXX dir/*.txt|wc -l >... (5 Replies)
Discussion started by: apenkov
5 Replies

7. Shell Programming and Scripting

How to append records to a file using PERL

Hi All, Great Forum and Great help. Keep up the good work. My question is what is the command and it's syntax to append a record to an output file using PERL. Please provide the command syntax. In regular shell you can use the '>>' to append. Basically, I am creating a small report... (1 Reply)
Discussion started by: nurani
1 Replies

8. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

9. UNIX for Dummies Questions & Answers

Output to file but append rather than overwrite?

I am running a command which has a parameter that outputs the results to a file each time it is run. Here is the command: --fullresult=true > importlog.xml Can I add the output to the file rather than creating a new one which overwrites the existing one? If not can I make the file name... (2 Replies)
Discussion started by: Sepia
2 Replies

10. Shell Programming and Scripting

Append output to file

Hi, I have a script below. It get's the data from the output of a script that is running hourly. My problem is every time my script runs, it deletes the previous data and put the current data. Please see output below. What I would like to do is to have the hourly output to be appended on the... (3 Replies)
Discussion started by: ayhanne
3 Replies
Login or Register to Ask a Question