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!

Please review my script....Thanks 2

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have been trying to work this out for three days now. I have two files involved.

master_sorted
pre_build_out_a#

I need to use the number at the end of the first pre_build_out_a# file, use that number to find that line number in the master_sorted file, and replace the text SITE_NUMBER in the pre_build_out_a# and output to pre_build_out_b# (# being the same number as in the original pre_build file)

It should keep processing files until it has completed all the prebuild files seperatly.


This is what I have so far, but it doesn't work.

BEGIN { FS =",";fn1="master_sorted"}
{
num = $5
fn2 = "pre_sorted_out_b" substr(FILENAME, MATCH(FILENAME "[0-9]+"))
while ((getline < fn1 ) > 0) {
gsub (/SITE_NUMBER/,num)
print > fn2
}
close(fn1)
close(fn2)
}



Contents of master_sorted:

11,11,152,223,B00222
16,16,116,23,B00056
296,96,132,10,B00332


Contents of pre_build_out_a1:

RXMOI:MO=RXOTG-152,COMB=HYB,RSITE=SITE_NUMBER,SWVER=B0531R0702;
RXESI:MO=RXOTG-152;
RXBLE:MO=RXOTG-152;
RXMOI:MO=RXOCF-152,TEI=TG_TEI;
RXMOC:MO=RXOCF-152,SIG=CONC;
RXMOI:MO=RXOIS-152;
RXMOI:MO=RXOTF-152,TFMODE=SA;
RXMOI:MO=RXOCON-152,DCP=64&&87;
DTBLE:DIP=T1_NUMBER;
EXDAI:DEV=DEVICE_RANGE;
BLODE:DEV=DEVICE_RANGE;
RXAPI:MO=RXOTG-152,DEV=DEVICE_RANGE,DCP=T1_DCPS;
 
This is untested but may help. Written for ksh.

for file in $(ls pre_build_out_a*)
do
num=$(echo $file | cut -c16-)
site=$(awk -v site=$site '$1==site {print $5}')
eval &quot;sed 's/SITE_NUMBER/$site/g' $file&quot; > pre_build_out_b${num}
done

Greg.
 
Told you it wouldn't work ... use nawk instead of awk :)

Greg.
 
how do you want me to run it?

nawk -f nawk.file ?????

 
No, it's a section of shell script to do what you asked above ...

#!/bin/ksh

for file in $(ls pre_build_out_a*)
do
num=$(echo $file | cut -c16-)
site=$(nawk -v site=$site '$1==site {print $5}')
eval &quot;sed 's/SITE_NUMBER/$site/g' $file&quot; > pre_build_out_b${num}
done

Greg.
 
I would really like it to work in awk. I like the post, but for my needs I need to use awk for this.
 
Ok, this is how I finally accomplished it. I modified some of vlad's previous post to get what I needed done:

/RXOTG/{
fn = FILENAME
sub(/^[^0-9]*/,&quot;&quot;,fn)
a = $1
sub(/.*-/,&quot;&quot;,a)
found = 0
while (!found && ((getline < &quot;master_sorted&quot;) > 0)) {
split ($0,b,&quot;,&quot;)
if (b[1] == a) found = 1
num = b[5]
}
close(&quot;master_sorted&quot;)
if (found) {
fn1 = &quot;pre_build_a&quot; fn
fn2 = &quot;pre_build__b&quot; fn
while ((getline < fn1) > 0) {
sub(/SITE_NUMBER/,num,$0)
print > fn2
}
close(fn1)
close(fn2)
}
}
 
Actually, I think some of yours was used also Marsd!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top