LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2013, 01:11 AM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
Post Perl: Save output into file


I would like to ask for help. I have a perl script to do a task (e.g. printf). I would like to read a text.txt which contains ID and x value. And I would like to save the output of printf into a new file. Any help?
So far, I have program until this stage, but it fails.

Code:
open (DATA, "<text.txt");
my $th = 0.000;
for ($th=0; $th<=5; $th+=0.001)
{
 sysopen (FILE, "0_"+$th+"_text.txt", O_RDWR | O_CREAT);
 while(<DATA>){
  my($ID, $x) = split;
  if ($x >= $th)
  {
   printf FILE "%D\n", $ID;
  }
 }
 close(FILE);
}
close(DATA);
It gives me two type errors:
Code:
1. Filehandle FILE opened only for input at the "printf FILE "%D\n", $ID;"
2. Argument "O_SVWST" isn't numeric in sysopen at " sysopen (FILE, "0_"+$th+"_text.txt", O_RDWR | O_CREAT);"
Please help me. Thanks alot!

Last edited by eminempark; 09-12-2013 at 01:12 AM.
 
Old 09-12-2013, 01:51 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this simple example (open infile, write line to outfile):
Code:
#!/usr/bin/perl
use strict ;
use warnings ;

# open file for reading
open( INFILE, "<infile" )
  or die "Can't open infile : $!\n" ;

# open file for writing
open( OUTFILE, ">outfile" )
  or die "Can't open outfile : $!\n" ;

# do your stuff here
while ( <INFILE> ) {
  printf OUTFILE "%D\n", $_ ;
}

# close both filehandles
close( OUTFILE ) ;
close( INFILE ) ;

exit 0 ;
 
1 members found this post helpful.
Old 09-12-2013, 02:55 AM   #3
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
I would like to ask about the output file name. For example, I would like to name my file output from the variable $th. How can I do that? Because from the code that you provided, it can work if we already have the output file. But if we want to automatic create and based on the variable $th? Thank you very much.
 
Old 09-12-2013, 03:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by eminempark View Post
I would like to ask about the output file name. For example, I would like to name my file output from the variable $th. How can I do that?
Quote:
But if we want to automatic create and based on the variable $th?
Code:
#!/usr/bin/perl
use strict ;
use warnings ;

my $outfile = "/path/to/my.outfile" ;

open( INFILE, "<infile" )   # use hard coded file name
  or die "Can't open infile : $!\n" ;

open( OUTFILE, ">$outfile" )   # use variable instead of hard coded
  or die "Can't open $outfile : $!\n" ;

while ( <INFILE> ) {
  printf OUTFILE "%D\n", $_ ;
}

close( OUTFILE ) ;
close( INFILE ) ;

exit 0 ;
The above overwrites the outfile (single >), if you want/need to append to the outfile then use >> instead of >
open( OUTFILE, ">>$outfile" )

Quote:
Because from the code that you provided, it can work if we already have the output file.
Nope, that isn't true. output will be created if it isn't there:
Code:
$ ls -l
total 8
-rwxr-x--- 1 druuna druuna 280 sep 12 08:49 foo.pl
-rw-r----- 1 druuna druuna   6 sep 12 08:45 infile
$ ./foo.pl 
$ ls -l
total 12
-rwxr-x--- 1 druuna druuna 280 sep 12 08:49 foo.pl
-rw-r----- 1 druuna druuna   6 sep 12 08:45 infile
-rw-r----- 1 druuna druuna   6 sep 12 10:00 outfile
These might come in hand:
Perl:
 
Old 09-12-2013, 04:00 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
what do you mean by $th ? there is no such variable in perl. Do you want to specify the output file as an argument to the program?
 
Old 09-12-2013, 04:05 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
from OP
Code:
my $th = 0.000;
for ($th=0; $th<=5; $th+=0.001)
 
Old 09-12-2013, 04:10 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
oh, yes, you need to construct the filename like:
$outfile = "0_" . $th . "_text.txt";
 
1 members found this post helpful.
Old 09-12-2013, 04:38 AM   #8
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
May I know why are some my file name can be so weird? It is very long...

My code is some sort like this
Code:
#!/usr/bin/perl
use strict;
#use warnings;
my $th;
my $file_number;
for ($th=22.063; $th<=28.813; $th+=0.001)
{
open(INPUT, "<burst.txt")
  or die "Cant open infile :$!\n";
  
open(OUTPUT, "+>0_".$th."_burst.txt")
  or die "Cant open outfile :$!\n";
  

  while(<INPUT>){
    my($ID, $x) = split;
    my $vector = $x;
    if ($ID == 1 || $ID == 600)
    {
      printf OUTPUT "%d %.3f\n", $ID, $x;
    }
    else
    {
      if($vector >= $th)
      {
        printf OUTPUT "%d %.3f\n", $ID, $x;
      }
    }
  }
  close(OUTPUT);
}
close(INPUT);

exit 0;
Any idea?
Attached Thumbnails
Click image for larger version

Name:	Capture2.PNG
Views:	38
Size:	25.3 KB
ID:	13405  
 
Old 09-12-2013, 04:40 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you should open (and close) the file inside the while loop.
 
1 members found this post helpful.
Old 09-12-2013, 05:41 AM   #10
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
You mean the INPUT file or OUTPUT file or both?
 
Old 09-12-2013, 06:40 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you have only one input file that should be opened only once - that will happen before the while loop.
You have a lot of output files, all of them should be opened inside the loop - when you know the name
 
1 members found this post helpful.
Old 09-17-2013, 02:19 AM   #12
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
you have only one input file that should be opened only once - that will happen before the while loop.
You have a lot of output files, all of them should be opened inside the loop - when you know the name
It still produce the same file name. With alot 0 behind. any solution for this? I really need help.
Attached Thumbnails
Click image for larger version

Name:	Capture3.PNG
Views:	32
Size:	76.6 KB
ID:	13446  
 
Old 09-17-2013, 02:31 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
try:
$outputfile = sprintf "0_%1.3f_burst.txt", $th;
open(OUTPUT, "+>$outputfile") or die "Cant open outfile :$!\n";
 
1 members found this post helpful.
Old 09-17-2013, 02:56 AM   #14
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
After I change to this code

Code:
#!/usr/bin/perl
use strict;
#use warnings;
my $th;
for ($th=22.000; $th<=22.005; $th+=0.001)
{
open(INPUT, "<burst.txt")
  or die "Cant open infile :$!\n";
  while(<INPUT>){
  my $outputfile = sprintf "0_%.3f_burst.txt", $th;
  open(OUTPUT, "+>$outputfile")
  or die "Cant open outfile :$!\n"; 
    my($ID, $x) = split;
    #my $vector = $x;
    if ($ID == 1 || $ID == 600)
    {
      printf OUTPUT "%d %.3f\n", $ID, $x;
    }
    else
    {
      if($x >= $th)
      {
        printf OUTPUT "%d %.3f\n", $ID, $x;
      }
    }
      close(OUTPUT);
  }

}
close(INPUT);
it unable to process the data correctly. It will only print out ID 600. It will not even print out ID 1.Why?
 
Old 09-17-2013, 03:50 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
we have still no info about the data you want to handle. please uncomment the line #use warnings and fix problems. Also you may need to check if split was successful.
"unable to process" is not enough to help you, please specify what have you got and what did you expect.
 
  


Reply

Tags
file, input, output, perlscript



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
Perl: Save output into file eminempark Linux - Software 2 09-12-2013 01:57 AM
save output of bash file sysmanz Linux - Software 6 07-11-2011 08:26 AM
[SOLVED] Sed – how do I save output to file with filename from content of another file? misarab Programming 4 02-28-2011 02:12 PM
Save output of libmemusage.so into a file Consystor Linux - Software 1 07-12-2009 04:29 AM
How to modify a field in few lines in a file and save the new file - in Perl rounak94 Programming 1 10-02-2008 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 PM.

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
Open Source Consulting | Domain Registration