chaney123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, below is the code that I've written. The first step is to unzip the files and later on convert them to csv files. So the question is, how to use Getopt to get to the final output without outputting the unzipped files? Now when I run the script, the clock.sum.rpt.txt and import.clock.rpt.txt will be produced. How to sort of hide them?

use strict; use warnings; use Getopt::Long qw(GetOptions); use IO::Uncompress::Gunzip qw(gunzip $GunzipError); #Unzip .gz files into .txt files # In command line run # perl getOptsLong -orig <file1_name> -new <file2_name> #GetOptions( # 'orig = s'=> $input_1 # 'new = s' => $input_2 # 'out = s' => $outfile_2 #); my $input_1 = "clock.sum.rpt.gz"; my $output_1 = "clock.sum.rpt.txt"; gunzip $input_1 => $output_1 or die"gunzip failed:$GunzipError\n"; my $input_2 = "import.clock.rpt.gz"; my $output_2 = "import.clock.rpt.txt"; gunzip $input_2 => $output_2 or die "gunzip failed:$GunzipError\n"; #Convert to cvs open my $infile_1, "<", $output_1 or die $!; open my $outfile_1, ">", "clock.sum.rpt.csv" or die $!; #$header_1 = "Clock,Sinks,Buffers,Cells,Slew,Path,Vio,Area"; my $lines_1; while(<$infile_1>){ if(/(\w+|\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\ +d+)\s+(\d+.\d+)/g){ tr/ /,/s; print $outfile_1 "$_"; $lines_1 = $_; }else{ #$lines_1 = $_; #warn "WARN: This line does not follow the normal output pattern, +"; } } #print $outfile_1 $header_1; print $outfile_1 "$lines_1\n"; close $output_1; close $outfile_1; open my $infile_2, "<", $output_2 or die $!; open my $outfile_2, ">", "import.clockrpt.csv" or die $!; my $lines_2; #my @columns_2 = ('Clock','Time','Waveform','Types','Sources'); while(<$infile_2>){ if(/(\w+)\s+(\d+.\d+)\s+(.\d+\s+\d+.\d+.)\s+/g){ tr/ /,/s; print $outfile_2 "$_"; $lines_2 = $_; }else{ #$lines_2 .= $_; #warn "WARN: This line does not follow the normal output pattern, +"; } } #print $outfile_2 "@columns_2\n"; print $outfile_2 "$lines_2\n"; close $output_2; close $outfile_2;

Replies are listed 'Best First'.
Re: How to use Getopt?
by huck (Prior) on Oct 03, 2017 at 01:48 UTC

    use strict; use warnings; use Getopt::Long qw(GetOptions); use IO::Uncompress::Gunzip qw(gunzip $GunzipError); #Unzip .gz files into .txt files # In command line run # perl getOptsLong -orig <file1_name> -new <file2_name> my $input_1 = ''; my $input_2 = ''; GetOptions( 'orig=s'=> \$input_1, 'new=s' => \$input_2 ) or die("Error in command line arguments\n"); unless ($input_1) {die "need to specify -orig";} unless ($input_2) {die "need to specify -new";} unless (-f $input_1) {die "no file '$input_1'";} unless (-f $input_2) {die "no file '$input_2'";} my $base_1 = $input_1; $base_1=~s/\.gz$//i; my $base_2 = $input_2; $base_2=~s/\.gz$//i; my $output_1=$base_1.'.txt'; gunzip $input_1 => $output_1 or die"gunzip failed:$GunzipError\n"; my $output_2=$base_2.'.txt'; gunzip $input_2 => $output_2 or die "gunzip failed:$GunzipError\n"; my $csv_1=$base_1.'.csv'; #Convert to cvs open my $infile_1, "<", $output_1 or die $!; open my $outfile_1, ">", $csv_1 or die $!; #$header_1 = "Clock,Sinks,Buffers,Cells,Slew,Path,Vio,Area"; my $lines_1; while(<$infile_1>){ if(/(\w+|\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\ +d+)\s+(\d+.\d+)/g){ tr/ /,/s; print $outfile_1 "$_"; $lines_1 = $_; }else{ #$lines_1 = $_; #warn "WARN: This line does not follow the normal output pattern, +"; } } #print $outfile_1 $header_1; print $outfile_1 "$lines_1\n"; close $output_1; close $outfile_1; unlink $output_1 or die "cant unlink $output_1 $!"; my $csv_2=$base_2.'.csv'; open my $infile_2, "<", $output_2 or die $!; open my $outfile_2, ">", $csv_2 or die $!; my $lines_2; #my @columns_2 = ('Clock','Time','Waveform','Types','Sources'); while(<$infile_2>){ if(/(\w+)\s+(\d+.\d+)\s+(.\d+\s+\d+.\d+.)\s+/g){ tr/ /,/s; print $outfile_2 "$_"; $lines_2 = $_; }else{ #$lines_2 .= $_; #warn "WARN: This line does not follow the normal output pattern, +"; } } #print $outfile_2 "@columns_2\n"; print $outfile_2 "$lines_2\n"; close $output_2; close $outfile_2; unlink $output_2 or die "cant unlink $output_2 $!";

    To use Getopt::Long the receiving variables need to be defined first, notice the my's before the call

    When using Getopt::Long you do not pass the value but a pointer to the storage location, that is what \$input_1 rather than $input_1 passes

    Notice simple error checking for command line options

    Notice how i determine a base name by removing the .gz from the end. if no .gz is at the end the original input name is used. Notice how i then add a .txt and .csv to that name to get the output file names

    How to sort of hide them?
    i just deleted(unlinked) them when they were no longer needed

      Hi, thanks for the script. I really learn alot from it. But there is a minor problem when i run the script.

      #Convert to cvs open my $infile_1, "<", $output_1 or die $!; open my $outfile_1, ">", "clock.sum.rpt.csv" or die $!; #$header_1 = "Clock,Sinks,Buffers,Cells,Slew,Path,Vio,Area"; my $lines_1; while(<$infile_1>){ if(/(\w+|\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\ +d+)\s+(\d+.\d+)/g){ tr/ /,/s; print $outfile_1 "$_"; $lines_1 = $_; }else{ #$lines_1 = $_; #warn "WARN: This line does not follow the normal output pattern, +"; } } #print $outfile_1 $header_1; print $outfile_1 "$lines_1\n";

      It shows " Use of uninitialized value $lines_1 in concatenation (.) or string at script_1 line 54, <$infile_1> line 1323." and doesn't allow me to output the csv file. I checked and the line 1323 in my 1st file is a '1'. However this problem does not occur at my 2nd file. Help

        What does $lines_1 get set to if that massive regexp never matchs? Im willing to bet you also found $outfile_1 was empty.