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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multipie file comparison and replacement

Status
Not open for further replies.

segment

ISP
Jun 15, 2004
225
US
Paste... Awk... Perl... Bash... Stumped on this

I have two files (Asterisk)

File one:

[1234]
type=friend
username=1234
secret=1234
host=dynamic
context=admin
mailbox=1234@anywhere
callerid="Anywhere" <1234>
nat=no

File two:
1234 Somewhere

I need to ensure that entries in file_one match file_two...

Something similar to:
awk '/1234/{gsub(/Somewhere/, "Anywhere")};{print}' file_one #Note it has to check file_two for replacement values

Awk has to read columns 1 and 2 in file two, compare it to the entry in file one:

This is what I get:
[root@crashbox ]# awk '/1234@/{getline;print}' sip.conf
callerid="Anywhere" <1234>

extension=`awk '{print $1}' file_two` # get an extension
newname=`awk '{print $2}' file_two` # get the new name
oldvalue=`awk '/$extension@/{getline;print}'` # grep extension followed by an @ sign and print the next line

This is where I'm stuck at... Replacing... I need to make sure in file_one, if the value in the example Anywhere is not the same as it is in file_two (Somewhere), it needs to replace Anywhere to Somewhere.

Make sense?

Super uber props on the help on this one...

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Scratch file_one... The text on file_one looks like:

callerid="Anywhere" <1234>
adsi=no
mailbox=1234@anywhere
callwaiting=yes
echotraining=yes
echocancel=yes
echocancelwhenbridged=no
channel => 184

So...

awk '/adsi/{print x};{x=$0}' file_one

Will print callerid="Anywhere" <1234> now I just need to compare that to the output of file_two's $2 field which is:

awk '{print $2}' file_two

Which prints Somewhere...

file_one
callerid="Anywhere" <1234>

file_two
1234 Somewhere

RESULT (new_file)
callerid="Somewhere" <1234>

Keep in mind these are names so I may need to print
awk '{print $result,$2,$3}' > newfile

callerid="John Jones" <1234>

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Getting there... I separated things on my own to make it easier to do and easier on the eye...


awk '/admin/{getline;getline;print}' file_two >> contexts
prints out callerid="John Jones" <3456>

Now I need to do something like:

for i in `awk '/admin/{getline;getline;print}' contexts`

do

awk '{print "context=admin\nsignalling=fxo_ls\n"$i"\nadsi=no\nfaxdetect=no\nmailbox=???@host\ncallwaiting=yes\nechocancel=yes\nechocancelwhenbridged=yes\ncallwaiting=yes\nchannel => 10\n"}' >> newcontext

done

Where it would print:

context=admin
signalling=fxo_ls
callerid="John Jones" <3456>
adsi=no
faxdetect=no
mailbox=3456@host
callwaiting=yes
echocancel=yes
echocancelwhenbridged=yes
callwaiting=yes
channel => 10

There are 3000+ extensions ... Definitely a long time to edit...

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
How about this:

Code:
awk '
        NR==FNR { place[$1]=$2; next }
        /^[[][0-9]+[]]$/ { ext=$1; gsub("[][]","",ext); }
        /mailbox=/ { gsub("@.*","@"place[ext],$0) }
        /callerid=/ { gsub("\".*\"","\""place[ext]"\"" ,$0) }
        { print }
' file2 file1

Annihilannic.
 
Annihilannic:

Sort of. The thing is, for some it works, and others it doesn't. My guess is because of the fields

[root@mybox scripttest]# grep 7404 zapata.conf
;callerid="JONES, JIM" <7404>
;mailbox=7404@mybox

[root@mybox scripttest]# grep 7404 callerid.txt
7404 GoodhallJames

I will dabble with it in a while :( I have BGP issues though. Not cool on DS3 lines

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Looks like the following line is missing from zapata.conf:

[tt][7404][/tt]

Annihilannic.
 
zapata.conf never places an extension like that [7404] . The [] brackets are used for contexts in extensions.conf in asterisk's extensions.conf and for extensions in sip.conf here is an actual entry in my existing zapata.conf:

context=admin
signalling=fxo_ls
callerid="XXXXX,Nicole" <7214>
adsi=no
faxdetect=no
mailbox=7214@mybox
callwaiting=yes
echocancel=yes
echocancelwhenbridged=yes
callwaiting=yes
channel => 77

The problem is, when the prior admin configured the machine, his entries in voicemail.txt become (in a nice way)... idiotic

Here is a snippet:

7019 Emily XXXXX
7020 AccountingFax
7021 Med-Center
7022 Rec-Cent.Fax

So for someone's name I can '{print $2,$3}' ... Those come out fine, the others are loony. I'm replacing his insanity little by little, but I have 4 machines in four geographic areas and they all have their own methods of looking at each other. Its odd. But the answer to your question (did it work). Yes for those with normal entries: 7019 Emily XXXXX

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top