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

Posted on 2005-05-06
Medium Priority
Last Modified: 2010-10-05
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:


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
blah blah blah

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

Question by:hypnotx
  • 2

Author Comment

ID: 13948884
OK. I have half answered my own question. :)

Now I am having a problem with the following script:

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

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.)
LVL 45

Assisted Solution

sunnycoder earned 400 total points
ID: 13949019
for i in `cat mylist.txt`
sed "s/$i/# $i/g" file2.txt > newfile.txt

For your objective, you can leave out g from the sed expression .. Note the changed quotes
LVL 40

Accepted Solution

jlevie earned 400 total points
ID: 13949032
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

Author Comment

ID: 13949097
New point value and a split

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question