Modifying file from outside

hi ,

I have a javascript file like this:

function fnValidateId(){
        var array_id = new Array("444","7888","6633","555","146562","3333","33332")
        var tmp_id = document.form1.id.value;
            var num=0;
            while(1)
        {
                if(tmp_id == array_id[num])
            {
                return true;
            }
            else if(num == 7)

Now i want to (some way through shell script or awk) just pass the id to script and the modify the file to add the id to the array and increment the num by 1.

Where should "num" be incremented? I suppose it's on

right? If you add a new line it's supposed to become

Also,

Are my assumptions correct?

Yes you are absolutely correct.

Just change the "echo 11111" with your new id like echo new_id:

Tsunami js # echo 11111|perl -pe 'BEGIN { $id_new = <STDIN>; chomp($id_new); } s/(var array_id =.*?)\)/\1,\"$id_new\"\);/ if (/var array_id =(.*?)\)/); if (/else if\(num == (\d+)\)/) { $id_new_c = $1 + 1; s/(else if\(num == )\d+\)/\1$id_new_c\)/; }' js.js > js.js.new && mv js.js.new js.js
Tsunami js # cat js.js 
function fnValidateId(){
        var array_id = new Array("444","7888","6633","555","146562","3333","33332","11111");
        var tmp_id = document.form1.id.value;
            var num=0;
            while(1)
        {
                if(tmp_id == array_id[num])
            {
                return true;
            }
            else if(num == 8)
Tsunami js # 

It's a bit complex, probably not the way to go but it works perfectly. Remember that this code does not check if you're precisely changing fnValidateId(). It just searchers for text and changes it (could be within any function). Search "ajax" for other ways to do this without having to change the script itself.

create a script file as incr.sh :

var=`expr $(grep "num ==" file.html | awk -F= '{print $3}' | awk -F")" '{print $1}') + 1`

awk '/new Array/{gsub(/\)/,",\"'$1'\")")};{print}' file.html | awk '/num == /{gsub(/== [0-9]*/,"== '$var'")};{print}' > file.html

run the script incr.sh with input parameter ( the value u need to add).

i.e. incr.sh 8888

PS:You can add conditions to check if the input parameter is given, etc...

I am getting the below error

What operating system are you using? If it's Solaris (and possibly others) you may need to use /usr/bin/nawk or /usr/xpg4/bin/awk instead of the default /usr/bin/awk, because it is prehistoric.

mine is AIX
There is no issue in using awk.
Even the first statement works,
awk '/new Array/{gsub(/\)/,",\"'$1'\")")};{print}' file.html
the next statement is throwing error.

Try this instead maybe:

awk '/new Array/{gsub("\)",",\"'$1'\")")};{print}' file.html 

Thanks to all..
I changed the last statement

awk '/num == /{gsub(/== [0-9]*/,"== '$var'")};{print}'

to a simple sed statement

sed -e "s|num ==$count|num ==$var|g"

where count is the result of

count=$(grep "num ==" file.html|awk -F= '{print $3}'|awk -F")" '{print $1}') 

This is working fine now..