pythonstringsed

Replace substring if key is found in another file


I have files associated with people scattered around different directories. I can find them with a master file. Some of them need to be pulled into my working directory. Once I've pulled them, I need to update the master file to reflect the change. To keep track of which files were moved, I record the person's name in another file.

master.txt

Bob    "/home/a/bob.txt"
Linda  "/home/b/linda.txt"
Joshua "/home/a/josh.txt"
Sam    "/home/f/sam.txt"

moved.txt

Linda
Sam

Expected result of master.txt

Bob    "/home/a/bob.txt"
Linda  "/workingdir/linda.txt"
Joshua "/home/a/josh.txt"
Sam    "/workingdir/sam.txt"

I've tried

grep -f moved.txt master.txt | sed "s?\/.*\/?"`pwd`"\/?"
grep -f moved.txt master.txt | sed "s?\/.*\/?"`pwd`"\/?" master.txt
grep -f moved.txt master.txt | sed -i "s?\/.*\/?"`pwd`"\/?"

As an added complication, this is going to execute as part of a python script, so it needs to be able to work within a subprocess.run(cmd).


Update 1:

Based on some questions, here is what the relevant section of my Python code looks like. I'm trying to figure out what the next step should be in order to update the paths of the flagged files in master.

commands = ['program finder.exe "flaggedfile" > master.txt'
           ,'sed "\#"`pwd`"#d" list.txt | sed "s/:.*//" > moved.txt'
           ,'program mover.exe moved.txt .'
           #,'cry'
           ]
for cmd in commands:
     status = subprocess.run(cmd
                            ,cwd=folder
                            ,shell=True
                            ,stdout=subprocess.DEVNULL
                            ,stderr=subprocess.DEVNULL
                            )

"program" is a program that I work with, and "finder.exe" and "mover.exe" are executables for that program, which I'm using to locate flagged files and move into the working directory.


Solution

  • Frame challenge: I don't need to compare the two files, I can do sed shenanigans.

    1. Extract matching lines from master to have the entire line.
    2. Wrangle the lines to have just the path.
    3. Use sed grouping to copy the old path and filename, then build a sed command in place inside a new file.
    4. Execute the new file.

    This means that the python snippet looks like:

    commands = ['program finder.exe "flaggedfile" > list.txt'
               ,'sed "\#"`pwd`"#d" list.txt | sed "s/:.*//" > moved.txt'
               ,'program mover.exe moved.txt .'
               ,'grep -f moved.txt master.txt | grep -o "\/.*\.txt" | sed -r "s?^(.*/)(.*)?sed -i \\"s#\\1\\2#`pwd`/\\2#g\\" master.txt?g" > updatemaster.txt'
               ,'. ./updatemaster.txt'
               ]
    

    I tested this and it does work. Thank you to everyone for your advice. I understand that I have weird constraints that I'm working with, and I'm sorry that I can't use python properly because of it.