Shell scripting to use input from one file to do a find and replace in another.

I would like to create a shell script that takes a list from FILE_A and uses it as the input for a sed command that does an edit in FILE_B.
is there some way to grep each and every line of FILE_A and pass each line to sed to edit FO:E_B (or create a FILE_C' which would be safer)?

for example FILE_A conains a list of files:

...
/dir1/file1
/dir1/file2
..

FILE_B contains a bunch of information but will also have the references to files listed in FILE_A

...
blah blah this
blah blah that
/dir1/file1
blah blah blah
/dir1/file2
...

what I am basically looking for is a way to do:

sed 'a/<junk from FILE_A/<replace with edited junk from FILE_A>/g' FILE_B

TIA
hypnotxAsked:
Who is Participating?
 
jlevieCommented:
The sustitution in the sed command isn't working because you have it in single quotes. But there are larger problems with how you are trying to do this. The config file needs to contain all of the edits and that means that we need to operate on that data for each iteration of the loop. Also your for loop won't place each line of the list in the variable if there's a space in a line. A better approach would be to do:

cat mylist.txt | (while read; do
  mv file2.txt file2.txt.tmp
  sed -e "s/$REPLY/# $REPLY/" <file2.txt.tmp >file2.txt
done
0
 
hypnotxAuthor Commented:
OK. I have half answered my own question. :)

Now I am having a problem with the following script:

for i in `cat mylist.txt`
do
sed 's/$i/# $i/g' file2.txt > newfile.txt
done


This is what I am trying to do but I am having problems passing the $i variable to the sed command. I know that the for part is working because if I change the sed line to an 'echo $i' I can see what I want to pass to the sed command. Any help? (Yes, I am trying to comment lines out of a config file using the input from a list that I have created in a different file.)
0
 
sunnycoderCommented:
for i in `cat mylist.txt`
do
sed "s/$i/# $i/g" file2.txt > newfile.txt
done

For your objective, you can leave out g from the sed expression .. Note the changed quotes
0
 
hypnotxAuthor Commented:
New point value and a split
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.