![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Molecular biologist requires help re: search / replace script | gstuart | Shell Programming and Scripting | 11 | 04-09-2008 02:08 PM |
| search & replace password perl script | shellscript22 | Shell Programming and Scripting | 4 | 03-25-2008 11:17 AM |
| Need to search and replace in multiple files in directory hierarchy | umen | Shell Programming and Scripting | 3 | 12-24-2007 12:56 AM |
| Multiple search and replace | tungaw2004 | UNIX for Dummies Questions & Answers | 1 | 03-21-2007 08:03 PM |
| search and replace dynamic data in a shell script | csejl | Shell Programming and Scripting | 8 | 10-21-2003 07:33 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
multiple input search and replace script
hi,
i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files. cho "" echo "Domain_name " read domain_name echo "Work path Name : " read wk_path_name echo "Server Name 1: " read server1name echo "Admin Port: 1: " read adminport1 echo "Server Name 2: " read servername2 echo "Admin Port: 2: " read adminport2 echo "Multicast Addr :" read mcast_addr echo "Multicast Port :" read mcast_port for i in list.txt do cat $i |sed -e 's/oldpattern/$domain_name/g' $i > $i".new" done Problems 1. how to pass the values entered from "read" command thru the "oldpattern"? 2. how to pass the values without creating a new file? is there any way. |
| Forum Sponsor | ||
|
|
|
|||
|
For your qn1,
below one will be a solution... read oldval read newval while read line do { sed 's/'$oldval'/'$newval'/g' $line >$line".new" } done<list.txt For your qn2., I believe there is no other way other than creating a temp file which contains the output of sed command and then renaming it to the original filename... |
|
|||
|
Script to replace :
[root@unixguy ~]# cat replace.sh #!/bin/bash read oldval read newval while read line do { sed 's/'$oldval'/'$newval'/g' $line >$line".new" } done<list.txt Script I ran [root@unixguy ~]# ./replace.sh home-world sriram sed: can't read /home/sriram/test: No such file or directory ./replace.sh: line 3: $line".new": ambiguous redirect [root@unixguy ~]# cat test jshsyh hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js jshsyh hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js home-world hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js can u advice where i am going wrong ??? |
|||
| Google The UNIX and Linux Forums |