I am trying to search and replace using two different files with strict search rules.
One file contain some data and the other file contain some numbers as shown below.
DataFile.txt
>L1_T1
text data...
text data..
>L1_T1
text data...
text data..
>L1_T1
text data...
text data..
.....
.....
>L1_T2
text data...
text data..
>L1_T2
text data...
text data..
...
... upto
>L7_T8
text data...
text data..
>L7_T8
text data...
text data..
data header ranges from L1_T1 .. L1_T8 .. upto L7_T8 (56 headers altogether)
NumberFile.txt
1
3
4
7
8
75
86
...
...
4025
4030
4032
I want to replace the headers(L*_T*) of DataFile using the numbers from the NumberFile. However, the condition is each header has a particular range to take the numbers and one number can't be used twice. For Example:
L1_T1 can take those available numbers within the range 1 - 72
L1_T2 can take those available numbers within the range 73 - 144
L1_T3 can take those available numbers within the range 144 - 216
....
L1_T8 can take those available numbers within the range 505 - 576
....
...
L7_T8 can take those available numbers within the range 3961 - 4032
(Increment of 72 for each increasing Header)
Example Output for the above data can be:
>1
text data...
text data..
>3
text data...
text data..
>7
text data...
text data..
.....
.....
>75
text data...
text data..
>86
text data...
text data..
...
...
>4030
text data...
text data..
>4032
text data...
text data..
You already have ordered your datafile & number file ?
so in fact you just need to do the replacement step, not the checking rule step , it that correct ?
---------- Post updated at 12:31 PM ---------- Previous update was at 12:23 PM ----------
If so maybe you can try something like
Thanks for your reply.
Both the files are ordered. Thats right.
However, there are more numbers than records in the datafile.
For example, there are three L1_T1 headers, however, for this the numbers are 1,3,4,7 and 8.
So any three numbers can be used as long as they are within the range 1-72.
The code you provided converts:
>L1_T2 to >7
>L1_T2 to >8
which should be
>L1_T2 to >75
>L1_T2 to >86
(range for L1_T2 is 73 - 144)
also
L7_T7 to >4025
L7_T7 to >4030
(range for L7_T7 is 3961 - 4032)
Use nawk instead of awk if you are on a SunOS / Solaris plateform :
# cat numberf
1
3
4
7
8
75
86
560
2048
4025
4030
4032
# cat dataf
>L1_T1
text data...
text data..
>L1_T1
text data...
text data..
>L1_T1
text data...
text data..
.....
.....
>L1_T2
text data...
text data..
>L1_T2
text data...
text data..
...
... upto
>L7_T8
text data...
text data..
>L7_T8
text data...
text data..
# nawk 'NR==FNR{A[++i]=$1}/^>/{x=$1;gsub("[^0-9]*","",x);L=substr(x,1,1);T=substr(x,2,1);s=((8*L+T-9)*72+1);do{r=0;y=++j;if (s<=A[y] && A[y]<=s+71) r=sub($1,">"A[y],$0)}while(!r&&A[y]<s)}NR!=FNR' numberf dataf
>1
text data...
text data..
>3
text data...
text data..
>4
text data...
text data..
.....
.....
>75
text data...
text data..
>86
text data...
text data..
...
... upto
>4025
text data...
text data..
>4030
text data...
text data..