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!

i need to fix a phonenumber file and need some awk help

Status
Not open for further replies.

hlasaber

Technical User
Jan 15, 2002
3
US
howdy folks
i'm trying to keep up with the phonenumber
splits in my area and i need to figure out
how to take one number at a time from a file
like this
206
240
248
253
271
search for a matching exchange on a line
in a file like this
1000 313-981-3250
1080 313-671-2120
1120 313-326-4940
1440 313-522-1900
1480 313-841-1050
and if they match replace the area code
with the right one i.e.248
the first file is about 500 lines and the second is about 1500 lines. Any suggestions
you could make would be helpful.
thanks
 


awk '
function do_three_change(string,change, rep) {
str = substr(string,6,length(string))
rep = change substr(str,4,length(str))
return rep
}
BEGIN {
while ((getline all[x++] < &quot;file&quot;) > 0) {
++num_lines
}
}
{
for (i in all) {
split(all,array,&quot; &quot;)
if ($1 == array[1]) {
ick = $0
ok = do_three_change(ick,array[2])
gsub(ick,ok,$0)
}
}
print
}' file1

Assuming a bunch of things:

Your file formats are consistent:
The beginning action file is in the format:
1200 312
The second file looks like the one above:
1000 313-981-3250

And there is still the problem of reinstating the
original string.

Maybe one of the better guys can help you more. At least
it's a start.;)

 
Let me see if I understand this correctly. If any of the numbers in the first file match the 3 digits following the area code in the second file (981, 671, etc), you want to replace the area code 313 with 248. In the example data you gave, there would be no matches. Let me know if this is correct before I (or someone else) spends time writing a script for you. CaKiwi
 
Hey , Cakiwi..
&quot;Somebody&quot; already did....;)
 
Cakiwi
yes i know the numbers in sample wont match
this is just a head of the two files
the whole files are about 500 lines
to match 1500 phone numbers for about 5
splits
 
I don't know what the 5 splits are, but maybe this code will he helpful. Run it by entering

awk -f this-file file-2 < file-1

BEGIN {while ((getline nums[++ix] < &quot;-&quot;) > 0) ;}
{
for (ix in nums) {
if (nums[ix] == substr($2,5,3)) {
print $1 &quot; 248&quot; substr($2,4)
next
}
}
print
} CaKiwi
 
Sorry marsd, I didn't mean to ignore you, but it seemed that you were making some unwarranted assumptions about what hlasaber wanted. Also, there seems to be some errors in your code e.g. rep = change substr(str,4,length(str)) and you submitted it without turning off the procees TGML check box so that was interpreted as turing on italics. CaKiwi
 
Sorry, I Forgot to turn off TGML :~/ That should have read

... so that was interpreted as turing on italics.

CaKiwi
 
Hmm..
If you run the code on a matching set of input/output files
you will see the results.
str = substr(string,6,length(string))
str1 = substr(string,1,4)
rep = str1 &quot; &quot;change substr(str,4,length(str))
return rep
}
It's just string concatenation and manipulation on
basically fixed format data, I don't see your criticism.

The assumptions were best guesses in the absence of any
sort of matching criteria that made sense. The whole
problem still makes no sense.
And the algorithm works in most of the possible scenarios arising from the two file read/match/substitute problem
with only changes to the function and pat matching if condition.
BWTF do I know.


 
hey it makes sense.
you get a list from the phone company of exchanges that
have been moved from the 313 area code to the 248 area code
that list looks like this
206
240
248
253
271 only way longer.
you get a list from your boss of all the phone numbers known to your business that looks like this
1000 313-981-3250
1080 313-671-2120
1120 313-326-4940
1440 313-522-1900
1480 313-841-1050 only way,way longer
now you have to root out any exchange in your phonenumber file that matches the exchanges on the list from the phone company so you can correct the area code. That makes sense right.
anyhow you and Cakiw have given me some ideas and i'm greatful for the help

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top