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
$
$
Hello,
Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Hi,
I have two files: a.doc and b.txt
I wish to search the strings from file b.txt in a.doc and want to highlight them in a.doc with different colours using Perl or bash./awk/sed?
Please guide me. :)
Thanks!!!!! (10 Replies)
Hi ,
I have one file and in this file i have one like
TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;:
i want to replace this line with the following line
TEST1... (4 Replies)
Hi,
I have a file with values like this
1 11
2 11
3 44
4 55
5 66 (an representative of what I have).
I want to split this file into smaller files based on column 1 values (values within a range).
The issue that I am facing is that the file is really big, and takes too long to... (21 Replies)
Hi
I search CSV (deliminited by ,) for the string "test" in the second column and if matches print the hole line.
awk -F, '{ if($2=="\"test\"") print $0} ' test.csvHowto to the same in sed? I heared that sed is faster. Test.csv is 121 MB. (1 Reply)
Need help with either sed or awk to acheive the following
file1
-----
In the amazon forest
The bats eat all the time...
mon tue wed they would eat berries
In the tropical forest
The bats eat all the time...
on wed bats eat nuts
In the rain forest
The bats eat all the time...
on... (2 Replies)
I have a file which is
DFDG
START
DSFDS
DSDS
XXX
END (VIO)
AADD
START
SDSD
FGFG
END
and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Hi friends,
Could you please help me to resolve the below issue.
Input file :-
<Node>
<username>abc</username>
<password>ABC</password>
<Node>
<Node>
<username>xyz</username>
<password>XYZ</password>
<Node>
<Node>
<username>mnp</username>
... (3 Replies)
Hello,
i have a file like that
foo A new
bar A new
bar B new
I need to replace 'new' with 'done',
but only in lines containing 'bar' AND 'A'.
output file should then become
foo A new
bar A done
bar B new
Sorry im not able to figure it out, not even shure if i should take sed.... (10 Replies)
Here is my problem..
1. I want to search all those files with file name starting AJ128****
(in all the sub directories also)
2. I want to search for the follwoing type of string
line beging with string - 'AK*any_1_char*any_2_char*510'
3. I need to display list of file names... (2 Replies)