Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applying Math Formula to Awk Script

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
This is that last final part of my script that involves two math formulas.
Now I have learned soooo much from those of you that have helped me in
my recent posts. This is the very last final part, and it will be all
complete.

There are several files called pre_build_i# There is alot of text in
these files, but I am only concerned about a few lines. The text I
need to look at is similiar to below:

DTBLE:DIP=166RBLT;
EXDAI:DEV=DEV_ONE&&-DEV_TWO;
BLODE:DEV=DEV_ONE&&-DEV_TWO;
RXAPI:MO=RXOTG-62,DEV=DEV_ONE&&-DEV_TWO,DCP=5&&17;

The first formula is such:

Take the number next to RBLT and Multiply it by 24.

Take that number and add the first number next to the text: DCP=

Then that sum should be subtracted by 1.

Take that final number and put it in the place of the text DEV_ONE

So the first final number using the above example would be:

166 * 24 = 3984

3984 + 5 = 3989

3989 - 1 = 3988 (Use this number and replace the text DEV_ONE)



The second formula is such:

Take the number next to RBLT and Multiply it by 24.

Take that number and add the second number next to the text: && after the DCP=

Then that sum should be subtracted by 1.

Take that final number and put it in the place of the text DEV_TWO

So the second final number using the above example would be:

166 * 24 = 3984

3984 + 17 = 4001

4001 - 1 = 4000 (Use this number and replace the text DEV_TWO)

After replacing just output to a final file called final_prebuild_# using the
number of the input file as usual.

I should end up with a final file for each input pre_build_i# file like:

DTBLE:DIP=166RBLT;
EXDAI:DEV=3988&&-4001;
BLODE:DEV=3988&&-4001;
RXAPI:MO=RXOTG-62,DEV=3988&&-4001,DCP=5&&17;


That is the last last part of my 4-6 month quest to automate rehoming cell
sites automatically if I can have some help with the formula and script to finish.

I wanted to especially thank: CaKiwi , Vlad, and Marsd. I have learned so much in
the past 4 months from the three of you, everyone here is in the best hands with you!
 
couple of questions:
1. are there going to be multiple data 'blocks' of these?
If so, what signifies a 'block'?
2. Does the output line order matters?
3. Can 'RBLT' and 'DCP' tags appear on OTHER lines than specified in the sample file?

Maybe providing a 'richer' sample file could be helpful! vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
In each file there will be only one block of these. RBLT will not appear anywhere else in the input file. Although DCP will, but it will show up as DCP1 and DCP2 I am confused about the question:

&quot;Does the output line order matters?&quot;

An exact complete example of one of the input files is below:

RXMOI:MO=RXOTG-62,COMB=HYB,RSITE=150,SWVER=B0531R0702;
RXESI:MO=RXOTG-62;
RXBLE:MO=RXOTG-62;
RXMOI:MO=RXOCF-62,TEI=62;
RXMOC:MO=RXOCF-62,SIG=CONC;
RXMOI:MO=RXOIS-62;
RXMOI:MO=RXOTF-62,TFMODE=SA;
RXMOI:MO=RXOCON-62,DCP=64&&87;
DTBLE:DIP=150RBLT;
EXDAI:DEV=DEV_ONE&&-DEV_TWO;
BLODE:DEV=DEV_ONE&&-DEV_TWO;
RXAPI:MO=RXOTG-62,DEV=DEV_ONE&&-DEV_TWO,DCP=1&&24;

RXMOI:MO=RXOTRX-62-0,TEI=0,DCP1=128,DCP2=129&130;
RXMOI:MO=RXOTRX-62-1,TEI=1,DCP1=131,DCP2=132&133;
RXMOI:MO=RXOTRX-62-2,TEI=2,DCP1=134,DCP2=135&136;
RXMOI:MO=RXOTRX-62-3,TEI=3,DCP1=137,DCP2=138&139;
RXMOI:MO=RXOTRX-62-4,TEI=4,DCP1=140,DCP2=141&142;
RXMOI:MO=RXOTRX-62-5,TEI=5,DCP1=143,DCP2=144&145;
RXMOI:MO=RXOTRX-62-6,TEI=6,DCP1=160,DCP2=161&162;
RXMOI:MO=RXOTRX-62-TRU_SEVEN,TEI=7,DCP1=163,DCP2=164&165;
RXMOI:MO=RXOTRX-62-8,TEI=8,DCP1=166,DCP2=167&168;
RXMOI:MO=RXOTRX-62-9,TEI=9,DCP1=169,DCP2=170&171;
RXMOI:MO=RXOTRX-62-10,TEI=10,DCP1=172,DCP2=173&174;
RXMOI:MO=RXOTRX-62-TRU_ELEVEN,TEI=11,DCP1=175,DCP2=176&177;

RXMOC:MO=RXOTRX-62-0,SIG=CONC;
RXMOC:MO=RXOTRX-62-1,SIG=CONC;
RXMOC:MO=RXOTRX-62-2,SIG=CONC;
RXMOC:MO=RXOTRX-62-3,SIG=CONC;
RXMOC:MO=RXOTRX-62-4,SIG=CONC;
RXMOC:MO=RXOTRX-62-5,SIG=CONC;
RXMOC:MO=RXOTRX-62-6,SIG=CONC;
RXMOC:MO=RXOTRX-62-TRU_SEVEN,SIG=CONC;
RXMOC:MO=RXOTRX-62-8,SIG=CONC;
RXMOC:MO=RXOTRX-62-9,SIG=CONC;
RXMOC:MO=RXOTRX-62-10,SIG=CONC;
RXMOC:MO=RXOTRX-62-TRU_ELEVEN,SIG=CONC;

RXMOI:MO=RXOTX-62-0,BAND=GSM1900,MPWR=45,ANT=ALPHAA;
RXMOI:MO=RXOTX-62-1,BAND=GSM1900,MPWR=45,ANT=ALPHAA;
RXMOI:MO=RXOTX-62-2,BAND=GSM1900,MPWR=45,ANT=ALPHAA;
RXMOI:MO=RXOTX-62-3,BAND=GSM1900,MPWR=45,ANT=BETAA;
RXMOI:MO=RXOTX-62-4,BAND=GSM1900,MPWR=45,ANT=ALPHAB;
RXMOI:MO=RXOTX-62-5,BAND=GSM1900,MPWR=45,ANT=ALPHAB;
RXMOI:MO=RXOTX-62-6,BAND=GSM1900,MPWR=45,ANT=ALPHAB;
RXMOI:MO=RXOTX-62-TRU_SEVEN,BAND=GSM1900,MPWR=POWER,ANT=TX_ANTENNA;
RXMOI:MO=RXOTX-62-8,BAND=GSM1900,MPWR=45,ANT=ALPHAC;
RXMOI:MO=RXOTX-62-9,BAND=GSM1900,MPWR=45,ANT=ALPHAC;
RXMOI:MO=RXOTX-62-10,BAND=GSM1900,MPWR=45,ANT=ALPHAC;
RXMOI:MO=RXOTX-62-TRU_ELEVEN,BAND=GSM1900,MPWR=POWER,ANT=TX_ANTENNA;

RXMOI:MO=RXORX-62-0,RXD=AB,BAND=GSM1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N-0&&-7;
 
Here's my attempt.

BEGIN {
FileROOT=&quot;pre_build_&quot;
unknownNUM=&quot;999999&quot;;
}

FNR == 1 {
if (match(FILENAME, &quot;[0-9]&quot;)) {
n = substr(FILENAME, RSTART, RLENGTH)
output=FileROOT n
}
else
output=outputFileROOT unknownNUM
}
/RBLT/ {
n1=$0
sub(/.*=/,&quot;&quot;,n1)
sub(/RBLT.*/,&quot;&quot;,n1)
}
/DCP=/ {
a = $0
sub(/.*DCP=/,&quot;&quot;,a)
sub(/;/,&quot;&quot;,a)
split(a,b,&quot;&&&quot;)
n2 = n1*24+b[1]-1
n3 = n1*24+b[2]-1
sub(/DEV_ONE/,n2)
sub(/DEV_TWO/,n3)
}
{print > output} CaKiwi
 
First of all I'm happy for you beaster and I hope this works for you.

The problem with the calculation is that awk reads the input once.
With the records needing to be modified
inside the records that need to be read
awk does not get a chance to modify the interior records till too late.
This was addressed in another thread with
the can't read the file twice topic.

So here is my solution which is kind of
unique ;)


function DCPnum(str) {
sub(/&/,&quot;&quot;,str) ; sub(/;/,&quot;&quot;,str)
gsub(/[A-Z]/,&quot;&quot;,str) ; sub(/=/,&quot;&quot;,str)
return str
}


/^DTBLE/ {
buf = $0
sub(/:/,&quot;&quot;,buf) ; gsub(/;/&quot;&quot;,buf) ; gsub(/=/,&quot;&quot;,buf);
gsub(/[a-zA-Z]/,&quot;&quot;,buf)
sum = buf * 24
}

/^RXAPI/ {
split($0,arr1,&quot;,&quot;)
sum1 = DCPnum(arr1[3])
split(sum1,final,&quot;&&quot;)
sum2 = sum + final[1] - 1 ; sum3 = sum + final[2] - 1
}

{

if (!pat) {
if (sum2 && sum3) {
cmd = &quot;awk -v pat=&quot;sum2&quot; -v spat=&quot;sum3&quot; -f awkscriptname targetfile&quot;
system(cmd)
}
} else {
gsub(/DEV_ONE/,pat,$0)
gsub(/DEV_TWO/,spat,$0)
print > pre_build_i#
}
}

here is sample output:
awk -f special.awk scrap.txt
DTBLE:DIP=DTBLEDIP=166RBLT;66RBLT;
EXDAI:DEV=3988&&-4000;
BLODE:DEV=3988&&-4000;
RXAPI:MO=RXOTG-62,DEV=3988&&-4000,DCP=5&&17;
 
Wow Nice!

CaKiwi,
It looks like yours will work great, although there are a few DCP_ONE and DCP_TWO fields where it is not being replaced. The other numbers are exactly correct. Here is the output I got from the sample above^

DTBLE:DIP=150RBLT;
EXDAI:DEV=DEV_ONE&&-DEV_TWO;
BLODE:DEV=DEV_ONE&&-DEV_TWO;
RXAPI:MO=RXOTG-62,DEV=3600&&-3623,DCP=1&&24;

See above where it did not replace?

Marsd,
I tried your input as well, to see what I got and the output was with awk and nawk:

bham_mta@wepmas1> nawk -f nawk18_test pre_build_i*
nawk: syntax error at source line 3 in function DCPnum
context is
gsub(/[A-Z]/,&quot;&quot;,str) ; >>> sub(/= <<<
nawk: illegal statement at source line 3 in function DCPnum
nawk: syntax error at source line 10
bham_mta@wepmas1> awk -f nawk18_test pre_build_i*
awk: syntax error near line 1
awk: bailing out near line 1

 
Here's my next attempt. I'm assuming that there are always 3 lines that need changing and that they always immediately follow the RBLT line.

BEGIN {
FileROOT=&quot;pre_build_&quot;
unknownNUM=&quot;999999&quot;;
}

FNR == 1 {
if (match(FILENAME, &quot;[0-9]&quot;)) {
n = substr(FILENAME, RSTART, RLENGTH)
output=FileROOT n
}
else
output=outputFileROOT unknownNUM
}
/RBLT/ {
print > output
n1=$0
sub(/.*=/,&quot;&quot;,n1)
sub(/RBLT.*/,&quot;&quot;,n1)
getline a1
getline a2
getline a3
a = a3
sub(/.*DCP=/,&quot;&quot;,a)
sub(/;/,&quot;&quot;,a)
split(a,b,&quot;&&&quot;)
n2 = n1*24+b[1]-1
n3 = n1*24+b[2]-1
sub(/DEV_ONE/,n2,a1)
sub(/DEV_TWO/,n3,a1)
print a1 > output
sub(/DEV_ONE/,n2,a2)
sub(/DEV_TWO/,n3,a2)
print a2 > output
sub(/DEV_ONE/,n2,a3)
sub(/DEV_TWO/,n3,a3)
print a3 > output
next
}
{print > output} CaKiwi
 
Beaster I wrote with gawk as usual so it may be that
nawk will never work for you...

try the function call:
function DCPnum(str) {
sub(/&/,&quot;&quot;,str)
sub(/;/,&quot;&quot;,str)
gsub(/[A-Z]/,&quot;&quot;,str)
sub(/=/,&quot;&quot;,str)
return str
}

The problem now is getting the FILENAME into the
function, that shouldn't be a big deal:
cmd = &quot;awk -v pat=&quot;sum2&quot; -v spat=&quot;sum3&quot; -f awkscriptname &quot; FILENAME

Then wrap the script in a for loop.
for ii in pre_build_i*
do
awkscript $ii > destination file
done

If this doesn't do it you're in good hands with
vlad and cakiwi. It's too bad because it works
beautifully here ;)
 
CaKiwi,
That was it! It works perfect! You are the best, along with everyone else that has helped!

Thanks,
Beaster
 
Am I too late for da party? ;-(

Anyway for what it's worth! ;)

#--------------------------
BEGIN {
FS=OFS=&quot;,&quot;

FileROOT=&quot;pre_build_&quot;
unknownNUM=&quot;999999&quot;;

PATnum=&quot;[0-9]+&quot;
PATrblt=&quot;RBLT&quot;
PATTrblt=PATnum PATrblt

PATdev1=&quot;DEV_ONE&quot;
PATdev2=&quot;DEV_TWO&quot;

PATTdcp=&quot;DCP=&quot;
found=0
}

/^[ ]*$/ { next}

FNR == 1 {
if (match(FILENAME, &quot;[0-9]&quot;)) {
n = substr(FILENAME, RSTART, RLENGTH)
output=FileROOT n
}
else
output=outputFileROOT unknownNUM
}

{
if ( match($0,PATTrblt)) {
numRBLT=substr($0,RSTART,RLENGTH-length(PATrblt));
#printf(&quot;RBLT->[%d]\n&quot;, numRBLT);
found++;
print > output;
next;
}
}


$NF ~ PATTdcp && found {
split($NF, dcpARR, &quot;[=&;]&quot;)
dev1=dcpARR[2]+(numRBLT*24)-1;
dev2=dcpARR[4]+(numRBLT*24)-1;
for(i=1; i <= foundIDX; i++) {
gsub(PATdev1, dev1, line2modifyARR)
gsub(PATdev2, dev2, line2modifyARR)
print line2modifyARR > output;
delete line2modifyARR;
foundIDX--;
}
gsub(PATdev1, dev1, $0); gsub(PATdev2, dev2, $0)
print > output
found=0;
next;
}

{
if (found)
line2modifyARR[++foundIDX]=$0;
else
print > output
}

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top