Replacing a value with many different values



  • I’m trying to replace a word within a sentence with a series of different numbers from another document (creating a new line for each variant).

    • I have one text file which contains a single line. (Text File A)

    • Within that single line I have a component that I would like to replace.

    • I have another text file which contains multiple numbers in each row which are different. (Text File B)

    I’m trying to replace the component from Text File A, with every number instance from Text File B (Creating a new line for each varient in the process).

    Example:

    (Text File A)

    “The number [Number] is awesome!”

    (Text File B)

    3425
    6432
    7237
    8383

    Desired outcome:
    (replace [Number] with every different number from Text File B)

    “The Number 3425 is awesome!”
    “The Number 6432 is awesome!”
    “The Number 7237 is awesome!”
    “The Number 8383 is awesome!”

    How would I do this?



  • @Felix-Gladitz said in Replacing a value with many different values:

    I’m trying to replace the component from Text File A, with every number instance from Text File B

    I would look at this the opposite way and say I want to add text around every line with a number in it.
    So using the Replace function and a regex (regular expression) we have:
    Find What:(\d+)
    Replace With:"The Number \1 is awesome"!

    As it’s a regular expression the search mode MUST be regular expression. Have wrap round ticked or make sure the cursor is in the first position. Click on the “Replace All” button.

    See how that works for you. Since you did not enter the examples inside a black box the quotes you show here may not be what you intended. Of course if it’s all just dummy data then not to worry. For future reference we prefer example data to be entered and then selected, then push the </> button above the window. Then repeat showing the example data changed. it helps a lot more seeing what you prefer to get.

    Terry



  • @Felix-Gladitz

    Duplicate The number(space)(space)is awesome in text file A the same number of times as you have rows of numbers in text file B. Note that (space)(space) is just two space characters.

    Put your caret on the 3 in 3425 in text file B. Press and hold Shift and Alt while using the right and down arrows to select all of the numbers (until you cover with selection the 3 in 8383). Press Ctrl+c.

    Move back to the first line in text file A, and put your caret between the two space characters. Press Ctrl+v.

    EDIT: Terry’s reply is awesome as well. :-)

    EDIT: We await you telling us how the data you provided is NOT really representative of your true problem, and how because of this our suggested solutions will not work. :-)



  • Hey you two - thanks for your responses :)

    The data I provided is not really representative of my true problem, HOWEVER your suggestion (Alan) worked amazingly - thank you! :D



  • Hello,@Felix-Gladitz
    Please try this command, To Replacing a value with many different values

    Take a look at Sed. You can easily achieve your goal with only one command line

    sed -e “s/Text_1/TextA/” -e “s/Text1/TextB/” <your_file.txt>your_file_new.txt

    I hope this command will be useful to you.
    Thank you.


Log in to reply