Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl Output a file

Posted on 2014-12-10
2
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

610 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