Perl Output a file

Posted on 2014-12-10
Last Modified: 2014-12-10

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:

Question by:shragi
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
LVL 26

Accepted Solution

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.

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>) {
	### 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


Author Comment

ID: 40492612
Thanks Wilcoxon :)

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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 (…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 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