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!

awk pattern matching two files 1

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have peiced together most of what I could, but am stuck trying to find a
way to grab particular fields that I need from two files, then output to a
final file.

I have a multiple files called antenna# that looks like:

RXOTX-11-0 B00227A ALL GSM1900 BETAA 45
RXOTX-11-1 B00227A ALL GSM1900 BETAA 45
RXOTX-11-2 B00227A ALL GSM1900 BETAA 45
RXOTX-11-4 B00227B ALL GSM1900 BETAB 43
RXOTX-11-5 B00227B ALL GSM1900 BETAB 46
RXOTX-11-6 B00227B ALL GSM1900 BETAB 43
RXOTX-11-8 B00227C ALL GSM1900 BETAC 44
RXOTX-11-10 B00227C ALL GSM1900 BETAC 46

I have the same amount of files called build# that looks like:

RXMOI:MO=RXOTX-152-0,BAND=GSM1900,MPWR=POWER,ANT=ZERO_ALPHA;
RXMOI:MO=RXOTX-152-1,BAND=GSM1900,MPWR=POWER,ANT=ONE_ALPHA;
RXMOI:MO=RXOTX-152-2,BAND=GSM1900,MPWR=POWER,ANT=TWO_ALPHA;
RXMOI:MO=RXOTX-152-TRU_THREE,BAND=GSM1900,MPWR=POWER,ANT=THREE_ALPHA;
RXMOI:MO=RXOTX-152-4,BAND=GSM1900,MPWR=POWER,ANT=FOUR_ALPHA;
RXMOI:MO=RXOTX-152-5,BAND=GSM1900,MPWR=POWER,ANT=FIVE_ALPHA;
RXMOI:MO=RXOTX-152-6,BAND=GSM1900,MPWR=POWER,ANT=SIX_ALPHA;
RXMOI:MO=RXOTX-152-TRU_SEVEN,BAND=GSM1900,MPWR=POWER,ANT=SEVEN_ALPHA;
RXMOI:MO=RXOTX-152-8,BAND=GSM1900,MPWR=POWER,ANT=EIGHT_ALPHA;
RXMOI:MO=RXOTX-152-TRU_NINE,BAND=GSM1900,MPWR=POWER,ANT=NINE_ALPHA;
RXMOI:MO=RXOTX-152-10,BAND=GSM1900,MPWR=POWER,ANT=TEN_ALPHA;
RXMOI:MO=RXOTX-152-TRU_ELEVEN,BAND=GSM1900,MPWR=POWER,ANT=ELEVEN_ALPHA;

I would like a bit of awk script to scan the first input file antenna# file
for the text "RXOTX". It may find anywhere from 0-11 of them. They are
numbered by the last number after the final dash. like -0, -1, -2, and so
on.

When it finds that text, I would like it to grab $6 from each line and
replace the text "POWER" in the associated build# file (antenna1 and build1
must go together). The important part is that the $6 needs to be matched up
with the same RXOTX -0, -1, -2 number as in the antenna file.

So if it found RXOTX-11-2 and $6 was 44 inside the file antenna1, it would
match inside build# RXOTX-#-2 (the number after the first - should be
ignored, it may be different) and replace the text "POWER" with the $6 and
output to a new file called build_output_# (# being the same fn as used in
all the input files)

Would end up with:

RXMOI:MO=RXOTX-152-0,BAND=GSM1900,MPWR=45,ANT=ZERO_ALPHA;
RXMOI:MO=RXOTX-152-1,BAND=GSM1900,MPWR=45,ANT=ONE_ALPHA;
RXMOI:MO=RXOTX-152-2,BAND=GSM1900,MPWR=45,ANT=TWO_ALPHA;
RXMOI:MO=RXOTX-152-TRU_THREE,BAND=GSM1900,MPWR=POWER,ANT=THREE_ALPHA;
RXMOI:MO=RXOTX-152-4,BAND=GSM1900,MPWR=43,ANT=FOUR_ALPHA;
RXMOI:MO=RXOTX-152-5,BAND=GSM1900,MPWR=46,ANT=FIVE_ALPHA;
RXMOI:MO=RXOTX-152-6,BAND=GSM1900,MPWR=43,ANT=SIX_ALPHA;
RXMOI:MO=RXOTX-152-TRU_SEVEN,BAND=GSM1900,MPWR=POWER,ANT=SEVEN_ALPHA;
RXMOI:MO=RXOTX-152-8,BAND=GSM1900,MPWR=44,ANT=EIGHT_ALPHA;
RXMOI:MO=RXOTX-152-TRU_NINE,BAND=GSM1900,MPWR=POWER,ANT=NINE_ALPHA;
RXMOI:MO=RXOTX-152-10,BAND=GSM1900,MPWR=46,ANT=TEN_ALPHA;
RXMOI:MO=RXOTX-152-TRU_ELEVEN,BAND=GSM1900,MPWR=POWER,ANT=ELEVEN_ALPHA;
 

nawk -f build.awk build1

#----------------- build.awk
BEGIN {
FSantenna=" "
FSbuild=OFS=","

PATrxotx="RXOTX[-0-9]+"

antennaROOT="antenna"
match(FILENAME,"[0-9]+");
antennaFILE=antennaROOT substr(FILENAME,RSTART, RLENGTH);

FS=FSantenna;
while (getline < antennaFILE > 0)
if ($1 ~ PATrxotx) {
split($1, tmpARR, &quot;-&quot;);
rxotxARR[tmpARR[3]]=$6;
}

FS=FSbuild;
}

{
if (match($1, PATrxotx)) {
split(substr($1, RSTART, RLENGTH), tmp, &quot;-&quot;)
rxotxINST=tmp[3];
if (rxotxINST in rxotxARR)
sub(&quot;POWER&quot;, rxotxARR[rxotxINST],$3);
}
print
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Here's my attempt.

{
if (FNR==1) {
for (j=1;j<=11;j++) a[j] = 0
fn1 = FILENAME
sub(/[^0-9]*/,&quot;&quot;,fn1)
fn2 = &quot;build_output_&quot; fn1
fn1 = &quot;antenna&quot; fn1
while ((getline s < fn1) > 0) {
if (s ~ &quot;RXOTX&quot;) {
split(s,f1)
split(f1[1],n1,&quot;-&quot;)
a[n1[3]] = f1[6]
}
}
}
if ($0 ~ &quot;RXOTX&quot;) {
split($0,f1,&quot;,&quot;)
j = split(f1[1],n1,&quot;-&quot;)
#print j &quot; &quot; n1[1] &quot; &quot; n1[3] &quot; &quot; a[n1[3]]
if (a[n1[3]]) sub(/POWER/,a[n1[3]])
}
print > fn2
}

Run it by entering [n]awk -f this-script build*

Vlad, your solution, while more elegant than mine, can be run on only one file at a time. CaKiwi
 
CaKiwi,

correct - that was the assumption for this quick&dirty - a shell wrapper script would be needed to iterate through all the &quot;build*&quot; files. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
CaKiwi
Did you mean to use fn1 = twice? Because when I run it, it does not substitute POWER with $6. It outputs the correct number of files, but with no changes.
 
Yes, I did mean to use fn1 = twice. Remove the # from the debug print line and see what you get. CaKiwi
 
Ok here is what I get sir...

bham_mta@wepmas1> nawk -f nawktest pre_build_d*
3 RXMOI:MO=RXOTX 0
3 RXMOI:MO=RXOTX 1 0
3 RXMOI:MO=RXOTX 2 0
3 RXMOI:MO=RXOTX TRU_THREE
3 RXMOI:MO=RXOTX 4 0
3 RXMOI:MO=RXOTX 5 0
3 RXMOI:MO=RXOTX 6 0
3 RXMOI:MO=RXOTX TRU_SEVEN
3 RXMOI:MO=RXOTX 8 0
3 RXMOI:MO=RXOTX TRU_NINE
3 RXMOI:MO=RXOTX 10 0
3 RXMOI:MO=RXOTX TRU_ELEVEN
3 RXMOI:MO=RXOTX 0
3 RXMOI:MO=RXOTX 1 0
3 RXMOI:MO=RXOTX 2 0
3 RXMOI:MO=RXOTX 3 0
3 RXMOI:MO=RXOTX 4 0
3 RXMOI:MO=RXOTX 5 0
3 RXMOI:MO=RXOTX 6 0
3 RXMOI:MO=RXOTX 7 0
3 RXMOI:MO=RXOTX 8 0
3 RXMOI:MO=RXOTX 9 0
3 RXMOI:MO=RXOTX 10 0
3 RXMOI:MO=RXOTX TRU_ELEVEN
3 RXMOI:MO=RXOTX 0
3 RXMOI:MO=RXOTX 1 0
3 RXMOI:MO=RXOTX 2 0
3 RXMOI:MO=RXOTX TRU_THREE
3 RXMOI:MO=RXOTX 4 0
3 RXMOI:MO=RXOTX 5 0
3 RXMOI:MO=RXOTX 6 0
3 RXMOI:MO=RXOTX 7 0
3 RXMOI:MO=RXOTX 8 0
3 RXMOI:MO=RXOTX 9 0
3 RXMOI:MO=RXOTX 10 0
3 RXMOI:MO=RXOTX TRU_ELEVEN
bham_mta@wepmas1>
 
put a

print s

before the line

if (s ~ &quot;RXOTX&quot;) { CaKiwi
 
Ok I added

print s
if (s ~ &quot;RXOTX&quot;) {

Like so and still received the same output CaKiwi.
 
put a

print fn1

after the line

fn1 = &quot;antenna&quot; fn1

Is this the correct name of the antenna file? If not, correct it
CaKiwi
 
CaKiwi that worked perfect! I just made one change as instructed:

fn1 = &quot;antennas_power_&quot; fn1

That was my mistake! If I want to turn the print to the screen off, what should I put back?

 
Glad it worked. To turn the print to screen off ,remove or put a # in front of all print statements except

print > fn2

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top