I have a file with more than 50,000 lines of records and each record is 50 bytes in length.
I need to search every record in this file between positions 11-19 (9 bytes) and 32-40 (9 bytes) and in case any of the above 2 fields is alpha-numeric, i need to replace the whole 9 bytes of that field by a default numeric value(say, 888888888).
$
$ # display the contents of the data file
$ cat data
D00000000236778767878 745454545456785.7USA8762
D000000001SMF46567878 458795477876785.7RSA9763
D00000000223684589878 11254DUT7876785.7IND8762
D00000000898454667878 788987984876785.7FRA4765
D00000000214569787836 325455454576785.7ENG4568
D00000000236778767878 564878754876785.7ZIM8766
D000000009785DOT67878 66589455DOS6785.7KEN8963
D00000000236548767878 225456687876785.7PAK8761
D00000000998651233878 878965454876785.7BRA8764
D000000005423SOW67878 9685658TSB76785.7AUS8765
D000000008TUR59767878 55425TUR5976785.7ARG4669
D00000000412563767878 225635445256785.7EGP8766
D00000000779654267878 332256367876785.7GER8965
$
$ # display the contents of the Perl program
$ cat -n process.pl
1 #!perl -w
2 $old = "data"; # the data file that we begin with
3 $new = "data.new"; # temporary data file
4 $log = "log"; # log file to record only those records that will be modified
5
6 $DEFAULT = "888888888"; # default replacement value
7
8 open (OLD, "<", $old) or die "Can't open $old for reading: $!";
9 open (NEW, ">", $new) or die "Can't open $new for writing: $!";
10 open (LOG, ">", $log) or die "Can't open $log for writing: $!";
11 while (<OLD>) {
12 if (substr($_,10,9) =~ !/\D+/ or substr($_,31,9) =~ !/\D+/) {
13 # log the record, modify it, write to temp file
14 print LOG "$.\t$_";
15 substr($_,10,9) = substr($_,31,9) = $DEFAULT;
16 print NEW $_;
17 } else {
18 # simply write to temp file
19 print NEW $_;
20 }
21 }
22 close (OLD) or die "Can't close $old: $!";
23 close (NEW) or die "Can't close $new: $!";
24 close (LOG) or die "Can't close $log: $!";
25
26 # rename NEW file to OLD file, effectively overwriting the old file
27 rename($new, $old) or die "can't rename $new to $old: $!";
$
$ # Run the program
$ perl process.pl
$
$ # Check the modified data file
$ cat data
D00000000236778767878 745454545456785.7USA8762
D00000000188888888878 458798888888885.7RSA9763
D00000000288888888878 112548888888885.7IND8762
D00000000898454667878 788987984876785.7FRA4765
D00000000214569787836 325455454576785.7ENG4568
D00000000236778767878 564878754876785.7ZIM8766
D00000000988888888878 665898888888885.7KEN8963
D00000000236548767878 225456687876785.7PAK8761
D00000000998651233878 878965454876785.7BRA8764
D00000000588888888878 968568888888885.7AUS8765
D00000000888888888878 554258888888885.7ARG4669
D00000000412563767878 225635445256785.7EGP8766
D00000000779654267878 332256367876785.7GER8965
$
$ # Check the log file
$ cat log
2 D000000001SMF46567878 458795477876785.7RSA9763
3 D00000000223684589878 11254DUT7876785.7IND8762
7 D000000009785DOT67878 66589455DOS6785.7KEN8963
10 D000000005423SOW67878 9685658TSB76785.7AUS8765
11 D000000008TUR59767878 55425TUR5976785.7ARG4669
$
$