LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I redirect the output of a file to another file within the file (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-redirect-the-output-of-a-file-to-another-file-within-the-file-679326/)

prakash.akumalla 10-27-2008 08:32 AM

How can I redirect the output of a file to another file within the file
 
Hi,

I have tried this for almost two days and finally approached this forum.

I wrote a script to connect to database and print the values in some fields from a table.

Now I want to redirect the output of this script to another file within that file. Now i am transferring it by command line as

perl file1.pl >file2

But i want it to be in the file1 itself.

Here the name of the file should change based on the values i consider in the file1.

Please help me in solving this.

Thanks,
Prakash.

pixellany 10-27-2008 08:48 AM

I don't know what you mean by "file within a file". In the normal filesystem, files cannot contain other files.

with the redirection operators, you have two options:

> (redirect to a file) This will replace anything in an existing file, or will create it if it does not exist.
>> (redirect and append to a file) This appends to an existing file (and will also create the file if it does not exist.

john test 10-27-2008 11:12 AM

1. What is the Database you are working in?

2. Are you looking for a database function to extract information from Table1 and Create a New Table2 containing that extracted data?

3. Are you trying to name Table2 with the name of the Field you are extracting from Table1?

prakash.akumalla 10-28-2008 11:30 PM

Hi,

Thanks for your replies. What I am actually trying for is, to redirect the output of a perl script to another file. This can be done by running the perl script in the command line by giving


> (redirect to a file) This will replace anything in an existing file, or will create it if it does not exist.
>> (redirect and append to a file) This appends to an existing file (and will also create the file if it does not exist.


but this is not what i actually want. I want to do it inside the perl script which i am running.
I dont want to give it while running the script.


My next thing is that The database i am using is a database in which it has a field name hostname which contains different hostnames. they may be IP addresses or the names. Now what i want in this case is, I will run this script for a particular hostname. I will give this in the command name. The output should be redirected to the file whose name should be hostname.txt. this hostname should change as per the name i give in the command line.

Please help in achieving this.

Thanks,
Prakash.

Tinkster 10-28-2008 11:56 PM

Define a file-descriptor inside your script, and put that
into your print-statements...

open (FH, $path)
print FH "Your output here"



Cheers,
Tink

prakash.akumalla 10-29-2008 12:28 AM

Hi,

Thanks for your reply. I tried this but it does not work for me.

The modifications i made in the script are

open (FH, "/root/perl/perl.txt");

print " ............";
print " ............";

close FH;


this is how i used. Earlier I just used those two print statements. The output is printed on the console then. But now also its doing the same. Even the file perl.txt is not created. I tried in the other way by creating the file perl.txt manually and tried but no data is printed in that file.

Please check this once.

Thanks,
Prakash.

chrism01 10-29-2008 12:55 AM

Are you running this as root? Otherwise it won't be able to write to that area.
Anyway, here's a start
Code:

open(FH, ">", "/root/out.txt") or die "Can't open /root/out.txt: $!\n";
print FH "testing\n";
close(FH) or die "Can't close /root/out.txt: $!\n";

The '$!' will give you the error msg if it fails.

you might want to bookmark/read these

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

prakash.akumalla 10-29-2008 01:06 AM

Hi,
Thanks for your reply. I worked for some extent. It created a file perl.txt in that /root/perl

But the data which is given with print is printed on the console itself. Its not being written in the file perl.txt .

Please look at this once.

Thanks,
Prakash.

chrism01 10-29-2008 01:15 AM

Actually, if you use my code EXACTLY, it will write to the file, not the console. In your code, you've skipped the filehandle invocation on the print lines.

prakash.akumalla 10-29-2008 01:32 AM

Hi,

Your assumption is correct. Thanks once again. But i have kept the print statements in a while loop. Now after keeping the file handlers as shown below,

{
open (FH, ">", "/root/perl.txt") or die "Can't open /root/per/test.txt: $!\n";
print FH $result->{'ID01'}."\t";
print FH $result->{'ID02'}."\t";
print FH $result->{'ID03'}."\n";
close (FH) or die "Can't close /root/perl/perl.txt: $!\n";
}

Only one row is being written in the file. Please check at this once.

Thanks,
Prakash.

Disillusionist 10-29-2008 02:39 AM

open the file handle before the while loop
close the file handle after the while loop

chrism01 10-29-2008 02:41 AM

Now you tell us/me....
Why don't you tell us what you actually want and show us the whole program, it'll save time.
(I'm not going to rewrite for you though.)
Incidentally, in this case, you're using the output redirect (>) when it sounds like you should be using append(>>).
Its just like the bash redirects.
BTW, its filehandle, not file handlers (different meaning)

prakash.akumalla 10-29-2008 03:02 AM

Hi,

Sorry for not making my problem clear. I got my problem solved by giving open and close before while loop. I really thank you both guys for helping me.

thanks,
Prakash

Disillusionist 10-29-2008 03:05 AM

Why open and close the file for each loop?

prakash.akumalla 10-29-2008 03:34 AM

Hi,
Yes you are correct . I did not think this. Now a file is created. This rises a new doubt in me.
How can i create a tar file to it with in the file.

What I mean to say is i just want to get the tar file of the file which is created and delete the file after the tar file is created. Now when i give the command "ls" after executing the above script, I should be able to see only the tar file but not the text file.

How can we do this? Please suggest any help.

Thanks,
Prakash.


All times are GMT -5. The time now is 04:46 PM.