Find and replace using 2 text files as arrays.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace using 2 text files as arrays.
# 1  
Old 06-11-2011
Find and replace using 2 text files as arrays.

Here's the nonfunctional code I have so far

Code:
 
#!/bin/bash
searchFor=(`cat filea.txt` )
replaceWith=(`cat fileb.txt`)
myMax=${#searchFor[@]}
myCounter=1
while [ $myMax != $myCounter ];
do sed -i 's/${$searchFor[$myCounter]}/${$replaceWith[$myCounter]}/g'
done

The goal is to use each line in filea.txt as a search term, and each line in fileb.txt as a replacement term. Then I would like to point the script toward filec.txt and replace all of the terms.
# 2  
Old 06-12-2011
Try this:
Code:
myifs="|"
paste -d $myifs filea.txt fileb.txt | while IFS=$myifs read a b
do
        sed -i "s/$a/$b/g" filec.txt
done

# 3  
Old 06-12-2011
one problem: there are spaces in "thing 1" and "replacement 1"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find text and replace next line.?

Have multiple files with the same format and certain text is the same. Have specific text to search for and then need to replace the next line. Can search on the text DEVICE and multiple lines will be found. The line after each DEVICE line has {. Want to replace the line { with {-someadditiontext.... (2 Replies)
Discussion started by: bigdaddywags
2 Replies

2. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

3. Shell Programming and Scripting

find from file and replace with specific text

Dear All, I do not have any knowledge of scripting. I want to replace specific lines of a text file with a specific text. Like I have one file which is "original file" and one file "changes file" which has list of lines which I want to replace in original file with a specific string. I want the... (5 Replies)
Discussion started by: libras
5 Replies

4. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

5. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

6. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

7. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

8. UNIX for Dummies Questions & Answers

Find and replace text PLEASE HELP

Dear friends please help, I have a large data file with few hundred lines. A small example is shown below: datafile is a file with few hundred lines with the third column has many different character stings: test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh I need to... (6 Replies)
Discussion started by: bobo
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace text

test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh Please advise how can I replace the abc, bcd....with ABC, BCD.... (1 Reply)
Discussion started by: bobo
1 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question