Solved

Perl Output a file

Posted on 2014-12-10
2
274 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 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

11 Experts available now in Live!

Get 1:1 Help Now