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!

Stuck on text replacement AWK 2

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have multiple files named like below:

pre_build_out1
pre_build_out2
pre_build_out3

(may have more or less at anytime)

In these above files is the text:

TRU_ZERO
TRU_ONE
TRU_TWO
TRU_THREE
TRU_FOUR
TRU_FIVE
TRU_SIX
TRU_SEVEN
TRU_EIGHT
TRU_NINE
TRU_TEN
TRU_ELEVEN

In the second set of files:

file1
file2
file3

(may have more or less at anytime)

There is the text:

RXOTX-#-# (Example RXOTX-123-0)
RXOTX-#-# (Example RXOTX-123-6)
RXOTX-#-# (Example RXOTX-123-11)

There may be anywhere from 0-11, but no more for the last number.

I need a script to look in the files named file# one by one looking for the text RXOTX and grab the second number, in the case above it would be 0 , 6 , and 11. Take those numbers and replace them in the cooresponding pre_build_out# files.

So using file1, finding RXOTX-123-0, RXOTX-123-6, RXOTX-123-11 the script would go in to the pre_build_out1 and replace TRU_ZERO with 0, TRU_SIX with 6, and TRU_ELEVEN with 11.

The output file should be called pre_build_a_# using the same number as the pre_build_out# file.
 
Ooooooooooooohhhhhhhhhhh. Run the script against file* not pre_build_out* CaKiwi
 
Ok CaKiwi,
I see where the problem is. this script is working correctly now.

But the issue where it is putting the -121 in all the files is being caused by the nawk file from yesterday. Can you look back at the script from yesterday and see why it is grabbing the 121 that is $3 and sticking it in every file, then this will all be complete.
 
Here is another way of doing it, without a file loop.
You could play with this if you want.

function transtable(n, m,arr) {
arr[1] = "ONE"
arr[2] = "TWO"
arr[3] = "THREE"
arr[4] = "FOUR"
arr[5] = "FIVE"
arr[6] = "SIX"
arr[7] = "SEVEN"
arr[8] = "EIGHT"
arr[9] = "NINE"
arr[10] = "TEN"
arr[11] = "ELEVEN"

for (m=1 ; m <= 11; m++) {
if (n == m) {
print &quot;Matched at&quot;, m, &quot;\n&quot;
return arr[m]
}
}
return &quot;NULL&quot;
}

function strip1(nstr) {
sub(/RX.*-[0-9]+-/,&quot;&quot;,nstr)
sub(/[\t ][A-Za-z]+.*/,&quot;&quot;,nstr)
return nstr
}

function strip2(strng,pat) {
sub(/TRU_[A-Z]+?/,pat,strng)
return strng
}


function loader(fname, arr,x,longstr) {
while ((getline < fname) > 0) {
arr[x++] = $0
}
close(fname)

x = 0
for (x in arr) {
longstr = length(longstr) < 1 ? arr[x] &quot;\n&quot; : longstr&quot; &quot;arr[x] &quot;\n&quot;
}
return longstr
}

function parray(arr,x, p) {
while (p <= x) {
p++
print arr[p]
}
printf &quot;\n&quot;
return
}

BEGIN {
z = split(loader(&quot;/home/mars/pre_build_out1&quot;),parr,&quot;\n&quot;)
f = split(loader(&quot;/home/mars/file1&quot;),farr,&quot;\n&quot;)

for (i=0 ; i <= f ; i++) {
if (farr ~ /RXOTX-[0-9]+-.*/ && farr !~ /<RX.*/) {
nums[a++] = strip1(farr)
}
}

print &quot;BEFORE: &quot;
parray(parr,z)
for (ab in nums) {
ret = transtable(nums[ab])
print ret, num[ab]
if (ret != &quot;NULL&quot;) {
for (all in farr) {
if (parr[all] ~ ret) {
parr[all] = strip2(parr[all],nums[ab])
}
}
}
}
print &quot;AFTER:&quot;
parray(parr,z)
}


Sample run:
BEFORE:
RXMOI:MO=RXOTS-121-TRU_SIX-0&&-7;
RXMOI:MO=RXORX-121-TRU_TEN,RXD=AB,BAND=GSM1900,ANTA=TEN_ALPHA,ANTB=TEN_BETA;
RXMOI:MO=RXORX-121-TRU_ONE,RXD=AB,BAND=GSM1900,ANTA=ONE_ALPHA,ANTB=ONE_BETA;
RXMOI:MO=RXOTX-121-TRU_FIVE,BAND=GSM1900,MPWR=POWER,ANT=FIVE_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_NINE,SIG=CONC;
RXMOC:MO=RXOTRX-121-TRU_ZERO,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_FOUR,TEI=4,DCP1=140,DCP2=141&142;
RXMOC:MO=RXOCF-121,SIG=CONC;
RXMOI:MO=RXOTS-121-TRU_SEVEN-0&&-7;
RXMOI:MO=RXORX-121-TRU_ELEVEN,RXD=AB,BAND=GSM1900,ANTA=ELEVEN_ALPHA,ANTB=ELEVEN_BETA;
RXMOI:MO=RXORX-121-TRU_TWO,RXD=AB,BAND=GSM1900,ANTA=TWO_ALPHA,ANTB=TWO_BETA;
RXMOI:MO=RXOTX-121-TRU_SIX,BAND=GSM1900,MPWR=POWER,ANT=SIX_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_TEN,SIG=CONC;
RXMOC:MO=RXOTRX-121-TRU_ONE,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_FIVE,TEI=5,DCP1=143,DCP2=144&145;
RXMOI:MO=RXOIS-121;
RXMOI:MO=RXOTS-121-TRU_EIGHT-0&&-7;

RXMOI:MO=RXORX-121-TRU_THREE,RXD=AB,BAND=GSM1900,ANTA=THREE_ALPHA,ANTB=THREE_BETA;
RXMOI:MO=RXOTX-121-TRU_SEVEN,BAND=GSM1900,MPWR=POWER,ANT=SEVEN_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_ELEVEN,SIG=CONC;
RXMOC:MO=RXOTRX-121-TRU_TWO,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_SIX,TEI=6,DCP1=160,DCP2=161&162;
RXMOI:MO=RXOTF-121,TFMODE=SA;
RXMOI:MO=RXOTS-121-TRU_NINE-0&&-7;
RXMOI:MO=RXOTS-121-TRU_ZERO-0&&-7;
RXMOI:MO=RXORX-121-TRU_FOUR,RXD=AB,BAND=GSM1900,ANTA=FOUR_ALPHA,ANTB=FOUR_BETA;
RXMOI:MO=RXOTX-121-TRU_EIGHT,BAND=GSM1900,MPWR=POWER,ANT=EIGHT_ALPHA;

RXMOC:MO=RXOTRX-121-TRU_THREE,SIG=CONC;
RXMOI:MO=RXOCON-121,DCP=64&&87;
RXMOI:MO=RXOTS-121-TRU_TEN-0&&-7;
RXMOI:MO=RXOTS-121-TRU_ONE-0&&-7;
RXMOI:MO=RXORX-121-TRU_FIVE,RXD=AB,BAND=GSM1900,ANTA=FIVE_ALPHA,ANTB=FIVE_BETA;
RXMOI:MO=RXOTX-121-TRU_NINE,BAND=GSM1900,MPWR=POWER,ANT=NINE_ALPHA;
RXMOI:MO=RXOTX-121-TRU_ZERO,BAND=GSM1900,MPWR=POWER,ANT=ZERO_ALPHA;
DTBLE:DIP=T1_NUMBER;
RXMOI:MO=RXOTS-121-TRU_ELEVEN-0&&-7;
RXMOI:MO=RXOTS-121-TRU_TWO-0&&-7;
RXMOI:MO=RXORX-121-TRU_SIX,RXD=AB,BAND=GSM1900,ANTA=SIX_ALPHA,ANTB=SIX_BETA;
RXMOI:MO=RXOTX-121-TRU_TEN,BAND=GSM1900,MPWR=POWER,ANT=TEN_ALPHA;
EXDAI:DEV=DEVICE_RANGE;
RXMOI:MO=RXOTS-121-TRU_THREE-0&&-7;
RXMOI:MO=RXORX-121-TRU_SEVEN,RXD=AB,BAND=GSM1900,ANTA=SEVEN_ALPHA,ANTB=SEVEN_BETA;
BLODE:DEV=DEVICE_RANGE;
RXMOI:MO=RXOTS-121-TRU_FOUR-0&&-7;
RXMOI:MO=RXOTRX-121-TRU_SEVEN,TEI=7,DCP1=163,DCP2=164&165;
RXAPI:MO=RXOTG-121,DEV=DEVICE_RANGE,DCP=T1_DCPS;
RXMOC:MO=RXOTRX-121-TRU_FOUR,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_EIGHT,TEI=8,DCP1=166,DCP2=167&168;

RXMOI:MO=RXOTX-121-TRU_ONE,BAND=GSM1900,MPWR=POWER,ANT=ONE_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_FIVE,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_NINE,TEI=9,DCP1=169,DCP2=170&171;
RXMOI:MO=RXOTRX-121-TRU_ZERO,TEI=0,DCP1=128,DCP2=129&130;
RXMOI:MO=RXOTG-121,COMB=HYB,RSITE=SITE_NUMBER,SWVER=B0531R0702;
RXMOI:MO=RXOTX-121-TRU_ELEVEN,BAND=GSM1900,MPWR=POWER,ANT=ELEVEN_ALPHA;
RXMOI:MO=RXOTX-121-TRU_TWO,BAND=GSM1900,MPWR=POWER,ANT=TWO_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_SIX,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_TEN,TEI=10,DCP1=172,DCP2=173&174;
RXMOI:MO=RXOTRX-121-TRU_ONE,TEI=1,DCP1=131,DCP2=132&133;
RXESI:MO=RXOTG-121;
RXMOI:MO=RXORX-121-TRU_EIGHT,RXD=AB,BAND=GSM1900,ANTA=EIGHT_ALPHA,ANTB=EIGHT_BETA;

RXMOI:MO=RXOTX-121-TRU_THREE,BAND=GSM1900,MPWR=POWER,ANT=THREE_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_SEVEN,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_ELEVEN,TEI=11,DCP1=175,DCP2=176&177;
RXMOI:MO=RXOTRX-121-TRU_TWO,TEI=2,DCP1=134,DCP2=135&136;
RXBLE:MO=RXOTG-121;
RXMOI:MO=RXOTS-121-TRU_FIVE-0&&-7;
RXMOI:MO=RXORX-121-TRU_NINE,RXD=AB,BAND=GSM1900,ANTA=NINE_ALPHA,ANTB=NINE_BETA;
RXMOI:MO=RXORX-121-TRU_ZERO,RXD=AB,BAND=GSM1900,ANTA=ZERO_ALPHA,ANTB=ZERO_BETA;
RXMOI:MO=RXOTX-121-TRU_FOUR,BAND=GSM1900,MPWR=POWER,ANT=FOUR_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_EIGHT,SIG=CONC;

RXMOI:MO=RXOTRX-121-TRU_THREE,TEI=3,DCP1=137,DCP2=138&139;
RXMOI:MO=RXOCF-121,TEI=TG_TEI;



Matched at 1

ONE
Matched at 5

FIVE
Matched at 8

EIGHT
Matched at 4

FOUR
NULL
Matched at 2

TWO
Matched at 6

SIX
Matched at 10

TEN
AFTER:
RXMOI:MO=RXOTS-121- 6 -0&&-7;
RXMOI:MO=RXORX-121- 10 ,RXD=AB,BAND=GSM1900,ANTA=TEN_ALPHA,ANTB=TEN_BETA;
RXMOI:MO=RXORX-121- 1 ,RXD=AB,BAND=GSM1900,ANTA=ONE_ALPHA,ANTB=ONE_BETA;
RXMOI:MO=RXOTX-121- 5 ,BAND=GSM1900,MPWR=POWER,ANT=FIVE_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_NINE,SIG=CONC;
RXMOC:MO=RXOTRX-121-TRU_ZERO,SIG=CONC;
RXMOI:MO=RXOTRX-121- 4 ,TEI=4,DCP1=140,DCP2=141&142;
RXMOC:MO=RXOCF-121,SIG=CONC;
RXMOI:MO=RXOTS-121-TRU_SEVEN-0&&-7;
RXMOI:MO=RXORX-121-TRU_ELEVEN,RXD=AB,BAND=GSM1900,ANTA=ELEVEN_ALPHA,ANTB=ELEVEN_BETA;
RXMOI:MO=RXORX-121- 2 ,RXD=AB,BAND=GSM1900,ANTA=TWO_ALPHA,ANTB=TWO_BETA;
RXMOI:MO=RXOTX-121- 6 ,BAND=GSM1900,MPWR=POWER,ANT=SIX_ALPHA;
RXMOC:MO=RXOTRX-121- 10 ,SIG=CONC;
RXMOC:MO=RXOTRX-121- 1 ,SIG=CONC;
RXMOI:MO=RXOTRX-121- 5 ,TEI=5,DCP1=143,DCP2=144&145;
RXMOI:MO=RXOIS-121;
RXMOI:MO=RXOTS-121- 8 -0&&-7;

RXMOI:MO=RXORX-121-TRU_THREE,RXD=AB,BAND=GSM1900,ANTA=THREE_ALPHA,ANTB=THREE_BETA;
RXMOI:MO=RXOTX-121-TRU_SEVEN,BAND=GSM1900,MPWR=POWER,ANT=SEVEN_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_ELEVEN,SIG=CONC;
RXMOC:MO=RXOTRX-121- 2 ,SIG=CONC;
RXMOI:MO=RXOTRX-121- 6 ,TEI=6,DCP1=160,DCP2=161&162;
RXMOI:MO=RXOTF-121,TFMODE=SA;
RXMOI:MO=RXOTS-121-TRU_NINE-0&&-7;
RXMOI:MO=RXOTS-121-TRU_ZERO-0&&-7;
RXMOI:MO=RXORX-121- 4 ,RXD=AB,BAND=GSM1900,ANTA=FOUR_ALPHA,ANTB=FOUR_BETA;
RXMOI:MO=RXOTX-121- 8 ,BAND=GSM1900,MPWR=POWER,ANT=EIGHT_ALPHA;

RXMOC:MO=RXOTRX-121-TRU_THREE,SIG=CONC;
RXMOI:MO=RXOCON-121,DCP=64&&87;
RXMOI:MO=RXOTS-121- 10 -0&&-7;
RXMOI:MO=RXOTS-121- 1 -0&&-7;
RXMOI:MO=RXORX-121- 5 ,RXD=AB,BAND=GSM1900,ANTA=FIVE_ALPHA,ANTB=FIVE_BETA;
RXMOI:MO=RXOTX-121-TRU_NINE,BAND=GSM1900,MPWR=POWER,ANT=NINE_ALPHA;
RXMOI:MO=RXOTX-121-TRU_ZERO,BAND=GSM1900,MPWR=POWER,ANT=ZERO_ALPHA;
DTBLE:DIP=T1_NUMBER;
RXMOI:MO=RXOTS-121-TRU_ELEVEN-0&&-7;
RXMOI:MO=RXOTS-121- 2 -0&&-7;
RXMOI:MO=RXORX-121- 6 ,RXD=AB,BAND=GSM1900,ANTA=SIX_ALPHA,ANTB=SIX_BETA;
RXMOI:MO=RXOTX-121- 10 ,BAND=GSM1900,MPWR=POWER,ANT=TEN_ALPHA;
EXDAI:DEV=DEVICE_RANGE;
RXMOI:MO=RXOTS-121-TRU_THREE-0&&-7;
RXMOI:MO=RXORX-121-TRU_SEVEN,RXD=AB,BAND=GSM1900,ANTA=SEVEN_ALPHA,ANTB=SEVEN_BETA;
BLODE:DEV=DEVICE_RANGE;
RXMOI:MO=RXOTS-121- 4 -0&&-7;
RXMOI:MO=RXOTRX-121-TRU_SEVEN,TEI=7,DCP1=163,DCP2=164&165;
RXAPI:MO=RXOTG-121,DEV=DEVICE_RANGE,DCP=T1_DCPS;
RXMOC:MO=RXOTRX-121- 4 ,SIG=CONC;
RXMOI:MO=RXOTRX-121- 8 ,TEI=8,DCP1=166,DCP2=167&168;

RXMOI:MO=RXOTX-121- 1 ,BAND=GSM1900,MPWR=POWER,ANT=ONE_ALPHA;
RXMOC:MO=RXOTRX-121- 5 ,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_NINE,TEI=9,DCP1=169,DCP2=170&171;
RXMOI:MO=RXOTRX-121-TRU_ZERO,TEI=0,DCP1=128,DCP2=129&130;
RXMOI:MO=RXOTG-121,COMB=HYB,RSITE=SITE_NUMBER,SWVER=B0531R0702;
RXMOI:MO=RXOTX-121-TRU_ELEVEN,BAND=GSM1900,MPWR=POWER,ANT=ELEVEN_ALPHA;
RXMOI:MO=RXOTX-121- 2 ,BAND=GSM1900,MPWR=POWER,ANT=TWO_ALPHA;
RXMOC:MO=RXOTRX-121- 6 ,SIG=CONC;
RXMOI:MO=RXOTRX-121- 10 ,TEI=10,DCP1=172,DCP2=173&174;
RXMOI:MO=RXOTRX-121- 1 ,TEI=1,DCP1=131,DCP2=132&133;
RXESI:MO=RXOTG-121;
RXMOI:MO=RXORX-121- 8 ,RXD=AB,BAND=GSM1900,ANTA=EIGHT_ALPHA,ANTB=EIGHT_BETA;

RXMOI:MO=RXOTX-121-TRU_THREE,BAND=GSM1900,MPWR=POWER,ANT=THREE_ALPHA;
RXMOC:MO=RXOTRX-121-TRU_SEVEN,SIG=CONC;
RXMOI:MO=RXOTRX-121-TRU_ELEVEN,TEI=11,DCP1=175,DCP2=176&177;
RXMOI:MO=RXOTRX-121- 2 ,TEI=2,DCP1=134,DCP2=135&136;
RXBLE:MO=RXOTG-121;
RXMOI:MO=RXOTS-121- 5 -0&&-7;
RXMOI:MO=RXORX-121-TRU_NINE,RXD=AB,BAND=GSM1900,ANTA=NINE_ALPHA,ANTB=NINE_BETA;
RXMOI:MO=RXORX-121-TRU_ZERO,RXD=AB,BAND=GSM1900,ANTA=ZERO_ALPHA,ANTB=ZERO_BETA;
RXMOI:MO=RXOTX-121- 4 ,BAND=GSM1900,MPWR=POWER,ANT=FOUR_ALPHA;
RXMOC:MO=RXOTRX-121- 8 ,SIG=CONC;

RXMOI:MO=RXOTRX-121-TRU_THREE,TEI=3,DCP1=137,DCP2=138&139;
RXMOI:MO=RXOCF-121,TEI=TG_TEI;




real 0m0.034s
user 0m0.020s
sys 0m0.000s


 
Well you both will be pleased to know I was the one who screwed it all up! Go figure.....

It has been running correctly, but I had been complaining that the number 121 was showing up in all three files. Well that was what it was supposed to do...If I had looked at my input file master_sorted I would have seen I set it up to look for $3 from the previous post, which all had 121 for $3. I have learned my lesson again!

Thanks for all the time and effort, both of you have been an enormous help!

Beaster

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top