want to replace some words with other from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to replace some words with other from a list
# 1  
Old 10-04-2008
MySQL want to replace some words with other from a list

I have a list file, in which both the words to be replaced and to be replaced with are there... need to run a script which will accept the list name and replace all the occurances ..
ex. the list file contains something like
hi=bye
go=come

Now i want to replace the words hi with bye and go with come...

Hope i am clear about what i am trying to say ... Smilie
# 2  
Old 10-04-2008
try somethig like this..
Code:
 
while read var
do
searchstring=`echo "$var"|awk -F= '{print $1}'`
replacestring=`echo "$var"|awk -F= '{print $2}'`
vi - inputfile <<!
:%s/$searchstring/$replacestring/g
:wq
!
done < listfile

or
Code:
 
while read var
do
searchstring=`echo "$var"|awk -F= '{print $1}'`
replacestring=`echo "$var"|awk -F= '{print $2}'`
sed "s/$searchstring/$replacestring/g" inputfilename > outputfilename
done < listfile

# 3  
Old 10-04-2008
if you have PHP
Code:
<?php
$lookup = "table";
$file = "file";
if ( $handle=fopen($lookup,"r") ) {
 while( ($line = fgetcsv($handle,4096,"=")) !== FALSE){
   $table[$line[0]]=$line[1];
 }
}
fclose($handle);
$data=file_get_contents($file);
foreach($table as $search => $replace){
    $data=str_replace($search,$replace,$data);
}
echo $data;
?>

# 4  
Old 10-04-2008
viyadhar85's solutions have the problem that they only do one substitution at a time, overwriting the result file on each iteration, resulting in outputfile containing inputfile with only the last replacements in the loop.

Code:
sed -e 's/^/s%/' -e 's/=/%/' -e 's/$/%g/' listfile |
sed -f - inputfile >outputfile

This creates a sed script out of the list file, and passes it to a second instance of sed. I hear some versions of sed don't accept -f - in which case you will need to use a different approach.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

How to replace multiple words together?

I am looking for any smart perl solution for multiple word replacement together. My awk is working but for big files it is superslow. awk 'NR==FNR {a=$2;next} {for ( i in a) gsub(i,a)}1' new_word_list.txt oldfile > newfile new_word_list.txt looks like AB733 ST756 AB734 ST219 AB11 ... (4 Replies)
Discussion started by: sammy777888
4 Replies

3. Shell Programming and Scripting

How to replace words in file?

Hi Guys, I have a text where we used Ram in 10 times now I want replace all Ram words by Shyam word then how to do it. (6 Replies)
Discussion started by: aaditya321
6 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Replace specific words with nothing

Hi I have a file like that contains infomation about genes exons introns made as a single string. i am just planning to get the gene name alone with out any extra information. intergenic_Nedd4_exon_0_F Gapvd1_intron_24_R Gapvd1_exon_25_Rmy output file should be intergenic_Nedd4 Gapvd1... (13 Replies)
Discussion started by: raj_k
13 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

How to replace words using VI editor on the fly

HI Guys, How Can I replace particular words in a file starting from the line where my cursor is pointing while the file is opened in VI editor? If you are using sed, please give me the code. Thanks (2 Replies)
Discussion started by: ajincoep
2 Replies

8. Shell Programming and Scripting

Replace words with the first characters

Hello folks, I have a simple request but I can't find a simple solution. Hare is my problem. I have some dates, I need to replace months with only the first 3 characters (jan for january, feb for february, ... all in lower case) ~$ echo '3 october 2010' | sed 3 oct 2010I thought of something... (8 Replies)
Discussion started by: tukuyomi
8 Replies

9. Shell Programming and Scripting

Replace all the words containing a particular pattern

Hi Please help with this one.. I have a file...I need to replace all the words containing a particular pattern say "xyz' and replace the entire containing xyz with abc.. Before xyzwork connect connect xyz disconnect raxyz After the operation i want something like below: abc... (4 Replies)
Discussion started by: ningy
4 Replies
Login or Register to Ask a Question