LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2008, 09:32 AM   #1
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Rep: Reputation: 15
Thumbs up 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.
 
Old 10-27-2008, 09:48 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 10-27-2008, 12:12 PM   #3
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
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?
 
Old 10-29-2008, 12:30 AM   #4
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2008, 12:56 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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

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



Cheers,
Tink
 
Old 10-29-2008, 01:28 AM   #6
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2008, 01:55 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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
 
Old 10-29-2008, 02:06 AM   #8
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2008, 02:15 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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.
 
Old 10-29-2008, 02:32 AM   #10
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2008, 03:39 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
open the file handle before the while loop
close the file handle after the while loop
 
Old 10-29-2008, 03:41 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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)
 
Old 10-29-2008, 04:02 AM   #13
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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
 
Old 10-29-2008, 04:05 AM   #14
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Why open and close the file for each loop?
 
Old 10-29-2008, 04:34 AM   #15
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect to a file the output of a service? rtmex Linux - Newbie 5 04-27-2007 12:09 AM
output redirect back to file mathfeel Linux - Software 1 03-21-2007 01:28 PM
How to redirect output to a file? mus1402 Linux - Newbie 2 02-05-2006 10:42 AM
redirect screen output to file timbuck Linux - Software 5 12-09-2005 07:57 PM
output from cpio won't redirect to file rawii Programming 5 01-27-2004 02:49 PM


All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration