#!/usr/bin/perl -w use strict; # Enforce good programming rules use warnings; # Provides run-time warnings my $infile; # Variable for input file my $outfile; # Variable for output file my $filetype; # Variable to store whether the file is Standard or Enhanced my @newlines; # Array variable to hold line in incoming text file variable. if (not defined $ARGV[0]) { die "Usage: ReplaceNewLine.pl \n"; } else { $infile = $ARGV[0]; } open(INFILE, $infile) || die "$infile not found in current location.\n"; chomp(my @lines = ); close(INFILE); # Place header from source file into $header variable my $header = $lines[0]; my @field = split("~", $lines[1]); if ($field[14]) { $filetype = 'Enhanced'; } else { $filetype = 'Standard'; } push (@newlines, $header); if ($filetype eq 'Enhanced') { # Loop through each index in the @lines array. foreach my $line (@lines) { my @field = split("~", $line); if ($field[11] eq '000015') { $line =~ s/$field[12]/92/g; $line =~ s/~~~~~~~~~10~/~10~/g; push(@newlines,$line); } if ($field[11] eq '000030') { $line =~ s/$field[12]/46/g; $line =~ s/~~~~~10~/~10~/g; push(@newlines,$line); } if ($field[11] eq '000060') { $line =~ s/$field[12]/23/g; $line =~ s/~~~10~/~10~/g; push(@newlines,$line); } } # End foreach } # End if if (defined $ARGV[1]) { $outfile = $ARGV[1]; } else { $outfile = "resultfile.txt"; } open my $fh, '>', $outfile or die "Cannot opent $outfile\n"; foreach (@newlines) { print $fh "$_\n"; } close $fh;