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!

unix find and replace script 2

Status
Not open for further replies.

krishsays

IS-IT--Management
Dec 3, 2004
45
GB
Hi

I need to write a unix script which does the following

--------------------------------------
Properties file

Var1= XXX
Var2= YYY
--------------------------------------
-------------------------

File 1

Var1 = xxxx
Var2= yyyy

-----------------------
This script should search var1 and var2 in properties file and get those values and then search var1 and var2 in file1 and replace its value by the values achieved from first search. There are multiple file1.

Scenario is we want to change var1 and var2 values in properties file and then we want to run this script which reads the new values from properties files and then finds the var1 and var2 in multiple files and then updates the new value.
Please can somebody help in this.

Thanks in advance
Krishan
 
And what have you so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for your reply PHV

I was thinking of using the using SED to replace the new values , only problem with that is i will be running the sed even for properties and as well as html files and all the file names would have been hardcoded in the script. This I think is not a good solution.

I dont know how can I work in two kinds of file using a unix script. This script needs to pick a updated variable value from the properties file and need to update same variable value in other files. Now If this can be done for just two files( i mean pick up from properties and update in a html file) then I can write some loop for updating that to multiple files.

thanks
krishan

 
man find

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I checked the man find ..but could'nt find any option with which I can modify the file.

can you please let me know using which find " option" I can modify file.

Thanks,
krishan
 
hi PHV
This was that solution.

""
Say you have a file named changes.txt like this:
oldhostname=newhostname
oldurl1=newurl1
oldurl2=newurl2
...
Then you may consider a command like this:
awk -F'=' '{NR==FNR{a[$1]=$2;next}{x=$0;for(k in a)gsub(k,a[k],x);print x}' changes.txt /path/to/input > output
----------------

can you please give me little insight how can i use this in present scenario.

thanks.
krishan



 
My question was, did the solution posted in your previous thread solve the issue ? (as you never replied it ...)
 
Hi PHV ,
In that case my problem was solved. that solution has worked. I am really sorry for not replying. Thanks for that solution.

Anyway in the present case now I have reached to this point where following is the only thing required

sed -e 's/oldvalue/newvalue/' oldfile > newfile

as you can see this command will change the values in the oldfile and will create the newfile with new values. Now in my case I dont want the new file. i want to make changes in the oldfile itself and keep it. Please help in this.

Thanks,
krishan
 
echo '1,$s!oldvalue!newvalue!g'"\nwq" | ex -s /path/to/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

I am using the following code to make changes to all html files in the directory
-----------
#!/bin/bash

for x in `ls *.html`
do
sed -e 's/oldvalue/newvalue/' $x > (thats where i want help)
done
------------

it is finding all the html files and i am using combinatiion of for and sed to make changes to the file. can you please see if my approach is fine , also as I discussed in the last thread that while using the sed I want the changes to be done in the original file. Please let me know the syntex for that.

Thanks in advance,
krishan
 
for x in *.html
do
echo '1,$s!oldvalue!newvalue!g'"\nwq" | ex -s $x
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanks for yoour reply phv here is the script i am testing
----------
for x in *.html
do
echo '1,$s!1234!3456!g'"\nwq" | ex -s $x
done
-------------
it gives me the following error

"" Extra characters at end of "substitute" command ""

please help

thanks
krishan

 
try:
/bin/echo '1,$s!1234!3456!g'"\nwq" | ex -s "$x"

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I am using solaris 9 .does that make any difference why its not working for me.

I think " '1,$s!1234!3456!g'"\nwq " is substition part right ..?? where the error suggest the problem

thanks,
krishan
 
have you tried my suggestion?

some shells [ksh] have 'echo' builtin in which case the syntax for the builtin is somewhat different from the '/bin/echo'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi VG ,

thanks for your help.It really solved the problem. I used that command for testing purpose i created 1.html , 2.html , 3.html and used this command and it worked fine.

then I tested it on the actual files which has names as 40977.html , 40937.html and so on and I want to replace
""<form name="pp_online_html_form"action=" method="post">

with follwoing

<form name="pp_online_html_form"action=" method="post">

and i got the following error.
-------------
File exists - use "w! 40977.html" to overwrite
Line too long
----------------

this suggest me to use w! to overwrite

Please let me know what I need to change in the syntex.

Thanks in advance
krishan
 
Hi VG ,

I changed '" /bin/echo '1,$s!1234!3456!g'"\nwq" | ex -s "$x" ""

to

"" /bin/echo '1,$s!1234!3456!g'"\nwq!" | ex -s "$x"

and it worked fine .
Thankyou very much for your help.

PHV : Thanks to you also.

Krishan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top