Solved

Perl Output a file

Posted on 2014-12-10
2
275 Views
Last Modified: 2014-12-10
Hi,

I wrote a perl script that does the following things.

1) Inputfile is provided
2) split input file using \n
3) if a line starts with "EMP" string then split the line with "|" and get the first word which is EMP ID
4) there might be multiple lines that starts with EMP so get all EMP ID's and out put those to a txt file

I wrote the below script but this one is not working not sure where did I made the mistake.

Sample Input file:

05|AAAAA|BBBBBBBB|BBBBCVCCBVBCV
EMP|10000000001|00000078|ENG|1
300|03/03/2014|12/31/9999|0.00
05|ASDFASDFAAAA|BBASDFASDBBBBBB|BBASDFASDBBCVCCBVBCV
EMP|100000123134|000000723128|ENG|1
300|01/12/2014|11/3/9999|0.00
TESTSCRIPT.txt
0
Comment
Question by:shragi
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40492595
This should mostly fix your script.  The problem is your handling of the file.  "while (<X>)" is already reading the file line by line.

With the "for 2.." are you trying to skip the first line of the input file, the first two lines, or something else?
Also see the comments in the script about your output lines - they almost certainly aren't doing what you want.
#!perl

use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;

my $helpme = 0;
my $man = 0;

my $inputFileName = 'C:\\Testfile.txt';
my $outputFileName = 'C:\\Testfile_Amt.txt';

my $errorcode = 0;
my $DEBUG=0;

if(exists $ENV{DEBUG}) {
	$DEBUG = ($ENV{DEBUG} eq "") ? 0 : $ENV{DEBUG};
}

GetOptions('help' => \$helpme, 'man' => \$man, 'infile=s' => \$inputFileName, 'outfile=s' => \$outputFileName) or pod2usage(2);

pod2usage(1) if $helpme;
pod2usage(-verbose => 2) if $man;

die 'No input file name specified!' unless $inputFileName;
die 'No output file name specified!' unless $outputFileName;

open(INFILE, '<', $inputFileName) or die "Could not open input file: $!";
open(OUTFILE, '>', $outputFileName) or die "Could not open/create output file: $!";

while(<INFILE>) {
	chomp;
	### possible skip of first line or two
	if (/^EMP\|([^|]+)\|/) {
		my $empID = $1;
		### what are you trying to do with these lines?
		### they almost certainly don't do what you want
	    #print OUTFILE "I " . "0" x ($empID). "\n";
   	    #push(@employee, $empID);
		print OUTFILE "I ", $empID, "\n";
	}
}

close INFILE;
close OUTFILE;

exit $errorcode;

Open in new window

0
 

Author Comment

by:shragi
ID: 40492612
Thanks Wilcoxon :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now