I have a text file with text of this order;

str/4
<</Contents(100 cups)/(Date)
Colour red
<</Contents(080 bowls)/(Date)
Status used
Pack team
<</Contents(200 John)/(Date)
School house

And another text file with a list of words in the order;

Tree house
Colon format
Same variable

Now the question is, how do I search or match the text between "Contents(" and ")/" in each line, i.e.. 100 cups, 080 bowls, 200 John and replace it with the text in the corresponding line from my second file? The first instance to be replaced should be replaced with the text in the first line, second replacing second, and third replacing third. Not sure of an autohotkey script or notepad++ could help. The final result should look like;

str/4
<</Contents(Tree house)/(Date)
Colour red
<</Contents(Colon format)/(Date)
Status used
Pack team
<</Contents(Same variable)/(Date)
School house

Replace certain text in lines with each line another file.

  • 1
    This is straightforward with bash and sed: can you use these? – AFH Sep 6 at 17:32
  • No please. A bit new to programming. Any idea on how to use those? – Gideon Sep 6 at 17:35
  • Pease, don't cross post stackoverflow.com/q/52209990/372239 – Toto Sep 6 at 18:21
  • It's scripting, rather than programming. What tools were you expecting to use? NP++ and AHK are not suitable: the dynamic edits are dependent on an external file, and this sort of edit is only possible with scripting or a custom program, such as a fairly simple C program (or the equivalent in another language). If you're on Windows (as NP++ suggests), then it may be possible in a cmd batch file, but it would be a rather complex one; it may be easier in PowerShell, but I have done only very trivial tasks in PS, so I can't advise. – AFH Sep 6 at 20:32
  • Thanks anyway AFH. Still any advise or help with the scripting wound be very much appreciated. Am a bit more of a beginner I must say. – Gideon Sep 6 at 20:53
up vote 1 down vote accepted

Providing a dedicated solution for the given question would not really add much value to the superuser community but going to the more general level we can see that this is a concept of conditional replacement of ceratain strings based on a given rule and replacement array.

I put together couple of lines in JavaScript and placed it in html file so it can be run from any desktop. It allows playing with different scenarios.

<head>
    <title>Conditional replacement</title>
</head>

<body>
    Input:
    <br>
    <textarea rows="15" cols="50" id="input1">
</textarea>

    <textarea rows="15" cols="50" id="input2">
</textarea>
    <br>
    <br> Regex rule:
    <input type="text" id="pattern" size="42">
    <br>
    <br>
    <button type="button" onclick="myFunction()">Run and generate output</button>
    <br>
    <br> Output and errors:
    <br>
    <textarea rows="15" cols="50" id="output"></textarea>
    <textarea rows="15" cols="50" id="errors"></textarea>

    <script>
        function myFunction() {
            var i1 = document.getElementById("input1").value;
            var lines1 = i1.split("\n");
            var i2 = document.getElementById("input2").value;
            var lines2 = i2.split("\n");
            var rule = document.getElementById("pattern").value;
            var output = "";
            var errors = "";
            var j = 0;
            try {
                for (var i = 0; i < lines1.length; i++) {
                    if (lines1[i].search(rule) !== -1) {
                        var re = new RegExp(rule, "g");
                        if (lines2[j] === undefined) {
                            errors += "No replacement for line match: " + lines1[i];
                            output += lines1[i] + "\n"; // no change
                        } else {
                            output += lines1[i].replace(re, lines2[j]) + "\n";
                            j++;
                        }
                    } else {
                        output += lines1[i] + "\n";
                    }
                }
            } catch (err) {
                errors += err.message + "\n";
            }
            document.getElementById("output").value = output;
            document.getElementById("errors").value = errors;
        }
    </script>
</body>

</html>


The rule is defined in regex so it offers more flexibility.

Let’s just use as an example the problem defined in the question.

We need to write a rule which will match the substrings that need to be replaced.

(?<=<<\/Contents\()[^^]*?(?=\)\/\(Date\))

enter image description here

Now it is time to fill the inputs and run the script.

enter image description here

I don't see why AHK wouldn't be suitable, apart from the demo variables here below it would be only 4 lines of code.

source= ; replace with FileRead
(
str/4
<</Contents(100 cups)/(Date)
Colour red
<</Contents(080 bowls)/(Date)
Status used
Pack team
<</Contents(200 John)/(Date)
School house
)

replace= ; replace with FileRead
(
Tree house
Colon format
Same variable
)

Loop, parse, replace, `n, `r
     source:=RegExReplace(source, "U)Contents\((.*)\)", A_LoopField,,1)

MsgBox % source 

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.