Modifying file from outside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modifying file from outside
# 1  
Old 08-20-2008
Modifying file from outside

hi ,

I have a javascript file like this:

Code:
HTML Code:
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.
Sreejith_VK
# 2  
Old 08-20-2008
Where should "num" be incremented? I suppose it's on
Quote:
else if(num == 7)
right? If you add a new line it's supposed to become
Quote:
else if(num == 8)
Also,
Quote:
new Array("444","7888","6633","555","146562","3333","33332")
is supposed to become
new Array("444","7888","6633","555","146562","3333","33332", "new_id")
Are my assumptions correct?
# 3  
Old 08-20-2008
Yes you are absolutely correct.
Sreejith_VK
# 4  
Old 08-20-2008
Just change the "echo 11111" with your new id like echo new_id:

Code:
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.
# 5  
Old 08-20-2008
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...
# 6  
Old 08-21-2008
I am getting the below error

Quote:
incr.sh 1222
syntax error The source line is 1.
The error context is
/num == >>> /{gsub(/= <<<
awk: The statement cannot be correctly parsed.
The source line is 1.
Sreejith_VK
# 7  
Old 08-21-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying listener file

Hi Pros, I'm writing a script to modify listener.ora file on multiple hosts. When I ssh to any server from a central server in our environment we are presented with menu to select the instance. I need to set my environment to listener which could be different number on every instance. How can I... (5 Replies)
Discussion started by: humble_learner
5 Replies

2. Shell Programming and Scripting

Modifying the header in a flat file

Hi, I need to modify the header of the flat file, can you provide me the code example: Temp.txt header: list_col1|list_col2|list_col3 data: 1|2|3 in the above header should be modified in the prescribed format(is available with me) Header: List_Col1|List_Col2|List_Col3 A... (4 Replies)
Discussion started by: srikanth_sagi
4 Replies

3. Shell Programming and Scripting

Modifying file to 75 characters

I have a text file containing some notes and I want to limit the lines to 75 characters. Tried using fold, however fold will cut words. Need something in bash, sed or awk to do this. Find the blank space less than 75 ant cut from there (10 Replies)
Discussion started by: kristinu
10 Replies

4. Infrastructure Monitoring

modifying existing file using C

Hi all, I have a snmpd.conf file as below. in "SECTION: Trap Destinations" line I want to add "trap2dest <IP>:162 <com_str>" on a new line. For this I wrote following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; ssize_t read_char_count = 0; ... (2 Replies)
Discussion started by: zing_foru
2 Replies

5. Shell Programming and Scripting

Modifying a file?

Hi, I want to convert a file that looks like this >joe XXXXXXXXXXXXXXXXXXX >man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS to something that looks like this (where the spacing is tab seperated) joe XXXXXXXXXXXXXXXXXXX man BBBBBBBBBBBBBBBBBBBBBSSSSSSSS I am able to do the reverse but the other... (3 Replies)
Discussion started by: kylle345
3 Replies

6. UNIX for Dummies Questions & Answers

Modifying a file using SED

Dear Members, I have a file which contains data as shown below: LOAD DATA APPEND INTO TABLE xxap.test ( RAW_DATA char(3000) "replace(:raw_data,'*','|')", LOADING_SEQUENCE "xx_gl_bank_fee_s1.nextval", CREATION_DATE SYSDATE, LAST_UPDATE_DATE SYSDATE, ATTRIBUTE1 "DATE_STAMP", ... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

7. Shell Programming and Scripting

modifying file using script

Hi, I am new to shell programming, and want to know is it possible to change the contents of file using script? for example, if want to search 2 words and want to replace these words with 2 new words. Regards, Manoj (4 Replies)
Discussion started by: manoj.solaris
4 Replies

8. UNIX for Dummies Questions & Answers

Modifying a .ksh file

Hi, i have created a simple .ksh file in the following manner cat <<EOF >mfile #!/bin/ksh echo "hello world" EOF I have 2 questions 1. now i would like to add a second line after the first echo command e.g. echo "this is line 2" how can i do that ? 2. I would then like... (1 Reply)
Discussion started by: corbusier
1 Replies

9. Shell Programming and Scripting

Help need in modifying the text of .txt file

Hi All, I've written a shell script in which i defined two varibles for example: str=1.0.0.15 timeStamp=2008.03.08 now using this varibles i need to modify a text file. The text content looks like this ************************ * packageNumber : 1.0.0.14 * * date :... (2 Replies)
Discussion started by: vinna
2 Replies

10. Shell Programming and Scripting

can someone help me with modifying this file

Hi, I have a file which has data kind of like this. Apr 13 08:20:38 uslis10a sendmail: m3DDKSx3006432: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKSoK006433: usliss26.ca.com Apr 13 08:20:38 uslis10b sendmail: m3DDKcSo006442: usliss26.ca.com Apr 13 08:20:38 uslis10c... (2 Replies)
Discussion started by: eamani_sun
2 Replies
Login or Register to Ask a Question

Featured Tech Videos