Find specific string in file 1 and replace in specific lines in file 2
-
Hi, i have a problem i’m not sure if can be solved with notepad++, but would be great if it does.
A i have two files that i need to find string in file 2, then find same info on file 1 and get information from file 1 (based on position in file 1) and replace it in file 2. In this link is image with sample data:
link text
The catch is that on file 2 there can be more than one line between the line with info found and the line that would be replaced.
Does anyone can give me some hints from where to start?
Thx a lot! -
@Denis-Sta said in Find specific string in file 1 and replace in specific lines in file 2:
Does anyone can give me some hints from where to start?
Well I’d say you are going to have to merge the 2 files if you want regex (regular expression) to do the work. That’s not as bad as it sounds. There have been several discussions in the past on this forum regarding similar needs to yours. I think most have had solutions found using just regexes.
The alternative would be a scripting language, Python is one amongst many that Notepad++ supports. But the issue here will be to have someone create a program for you in that language or will you need to learn it yourself.
In broad terms I’d:
- Make the several lines in each file become one for each “record”
- Sequence up an ascending number at the start of each line, sort of a line number, odds for 1 file, evens for the other. The lines in file 1 may not be required to have an ascending number at the front but I’ve included it for the time being.
- Have at the front of each line in both files the appropriate sequences of text you will be using to find the lines.
- Sort the lines (once both files merged) in ascending order. This will “pair” records together.
- Use a regex to update the record in the line from file 2 from it’s “pair”.
- Use a regex to remove the lines from file 1 and to also remove the “sort text” at the front of line.
- Sort the lines again with the “line number” from step 2. This will put them back in “original order”.
- Pull apart the lines so they look as they did originally, but now with the additional information you wanted added.
I replied to someone else recently that often a “BIG” problem like yours looks insurmountable. That is until we break it down into little steps, each then becomes fairly simple to code.
Terry