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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
10-27-2008, 08:32 AM
|
#1
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Rep:
|
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.
|
|
|
10-27-2008, 08:48 AM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
|
10-27-2008, 11:12 AM
|
#3
|
Member
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Rep:
|
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?
|
|
|
10-28-2008, 11:30 PM
|
#4
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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.
|
|
|
10-28-2008, 11:56 PM
|
#5
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Define a file-descriptor inside your script, and put that
into your print-statements...
open (FH, $path)
print FH "Your output here"
Cheers,
Tink
|
|
|
10-29-2008, 12:28 AM
|
#6
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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.
|
|
|
10-29-2008, 12:55 AM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
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
|
|
|
10-29-2008, 01:06 AM
|
#8
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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.
|
|
|
10-29-2008, 01:15 AM
|
#9
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
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.
|
|
|
10-29-2008, 01:32 AM
|
#10
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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.
|
|
|
10-29-2008, 02:39 AM
|
#11
|
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
|
open the file handle before the while loop
close the file handle after the while loop
|
|
|
10-29-2008, 02:41 AM
|
#12
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
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)
|
|
|
10-29-2008, 03:02 AM
|
#13
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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
|
|
|
10-29-2008, 03:05 AM
|
#14
|
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
|
Why open and close the file for each loop?
|
|
|
10-29-2008, 03:34 AM
|
#15
|
Member
Registered: Jun 2008
Location: India
Posts: 109
Original Poster
Rep:
|
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 01:54 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|