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

Posted on 2005-05-06
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

    Author Comment

    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

    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

    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

    New point value and a split

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    script 11 130
    please append a correct root= boot option HELP! cant boot up linux server! 22 535
    linux $PWD 2 463
    Shell script for mvn build 2 60
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    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…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now