How to replace a line, which has a specific text, with the content of a specific line?



  • Hello! I’m trying to replace a line, which has a specific text, with the content of a specific line.

    I need all lines that have the text "name": “noname”,

    "name": "noname",
    

    to be replaced by the contents of Line 6.

    I tried and researched, but the most I got was to switch the two lines.

    Thanks in advance!



  • @Yudi_Kondo said in How to replace a line, which has a specific text, with the content of a specific line?:

    to be replaced by the contents of Line 6.

    The obvious answer should have been to use a simple regex replacement with the contents of line 6 in that regex.

    Since you should have been able to deduce that I can only gather there is more to this question than appears in your post.

    Do you need to complete this on several files, possibly even using the “Find in Files” option?

    If yes to using “Find in Files” then I’d recommend using a scripting language such as Python which is available within the Notepad++ environment. This will require some learning though.

    If needing to stick with regexes, this is a concept I think might work.

    1. Get the line 6 copied to the top of each file. This means the regex to perform the replacement will only need to look for the “name” noname" strings after line 1 is stored.

    2. Have the regex store line 1 and then continue searching through the file until it finds the “next” occurance of the strings and replace the line.

    3. As this will only replace 1 line at a time, the regex needs to be run multiple times until all occurances replaced.

    4. Finally remove the line 1 from each file.

    There was a discussion in this forum quite a while ago with a solution to this type of problem that performed the many replacements in 1 pass. In that case each replacement was a different string.

    I think you need to provide us with a better example and more information on whether this is 1 or many files. Is the first occurrence of “name” and “noname” ALWAYS going to occur after line 6? Also you state you did get a solution but it just switched the 2 lines, can you provide that as it will help us understand better?

    Terry



  • @Terry-R Ok. There are 300 files that need to be modified and lines with the text “name”: “noname”, always appear after line 6.

    Here one of the files:

    01{
    02  "id": "Abomasnow",
    03  "spawnInfos": [
    04    {
    05      "spec": {
    06        "name": "Abomasnow",
    07	  "growth": 6
    08      },
    09      "minLevel": 40,
    10      "maxLevel": 45,
    11      "typeID": "pokemon",
    12      "rarity": 8.0
    13    },
    14    {
    15      "spec": {
    16        "name": "noname",
    17	  "growth": 6
    18      },
    19      "stringLocationTypes": [
    20        "Land"
    21      ],
    22      "minLevel": 50,
    23      "maxLevel": 50,
    24      "typeID": "pokemon",
    25      "tags": [
    26        "safari"
    27      ],
    28      "rarity": 120.0
    29    }
    

    Here line 16 would become line 06

    And as for the solution that switched the lines, I was wrong. I used a macro the last time I edited these files



  • @Yudi_Kondo said in How to replace a line, which has a specific text, with the content of a specific line?:

    always appear after line 6

    Great, thanks for the example. I have another question. Based on your example it would appear the “name” “noname” line ONLY appears once in the remainder of the file, is this correct. If that is a yes then the solution will be a lot easier to create.

    Terry



  • This post is deleted!


  • @Terry-R Yes, “name” “noname” appears only once in each file.



  • I think I have it. I assume that given each line is prefixed by a number that you don’t want line 16 (as in your example) suddenly be changed to “06” just because the contents of line 06 are copied to line 16.
    So my regex (which should also work in the “Find in Files” option is
    Find What:(?-s)^(((.+)?\R){5})(\d+)(.+\R)(?s)(.+?)(?-s)^((\d+).+"name": "noname".+\R)(?s)(.+)
    Replace With:\1\4\5\6\8\5\9

    You will see I flip between (?-s) and (?s) several times. This allows the regex to traverse several lines simply in between line 06 and line we need to replace. I then continue to the end of the file, so in essence this regex will process the whole file, rewriting the entire contents back with the small change of line 06 written to where the original text needing replacement was.

    It worked for me in your example in an open tab. I’d suggest make a backup copy of the 300 or so files before unleashing it on all of them, then randonly checking some against the original contents.

    Terry



  • @Terry-R Thank you Terry! I tested it here and it worked. Editing these files will be much easier now. But what would the regex look like if the lines didn’t have prefix numbers



  • @Yudi_Kondo said in How to replace a line, which has a specific text, with the content of a specific line?:

    But what would the regex look like if the lines didn’t have prefix numbers

    The 2 (\d+) get removed, however this would then change the number of groups being stored so in the replacement line there would be lots of changes. My head hurts from the number of groups I had to create in the solution so I’m all THUNKED out currently! Do you really need that as a solution as you’ve just said that my solution already provided did work?!

    Actually, here’s an opportunity for you to test it out yourself. See what each group (\1, \2 etc) in the Replace With field relates to in the Find What field. Then remove the 2 (\d+) as I stated. Then change the group numbers in the Replace With field and see if you can get the correct answer. test it on some dummy data. A big hint is. Start counting left brackets ( ONLY. This is what determines the group number. Don’t count the brackets for the ?-s and ?s.

    Terry



  • @Terry-R I just asked out of curiosity in case I needed something similar in the future. Thanks again Terry!



  • Here the regex without the 2 (\d+)

    (?-s)^(((.+)?\R){5})(.+\R)(?s)(.+?)(?-s)^(.+“name”: “noname”.+\R)(?s)(.+)

    \1\4\5\4\7



  • You are right, however if you copy back what shows on your post it probably won’t work. I think the quotes have been nobbled by the markdown interpreter. We have all suffered from this issue and one way to resolve it is to put special characters around the code so the interpreter does NOT alter the characters at all.

    In the same way as you provided your 2nd example and I provided my regex, these can prevent the problem. Just inserting the code as you would text (as you did above) has (according to my test) affected the code.

    But good on you for trying it out. I got my code and removed the 2 (\d+) bits as I stated and then used your \1\4\5\4\7 and got the answer for when your file does NOT include line numbers.

    Terry



  • It can be like that?

    ^(((.+)\R){5})(\d+)(.+\R)(?s)(.+)(?-s)^(..)(.+“name”: “noname”.+\R)(?s)(.+)
    \1\4\5\6\7\5\9

    Here I learn from the best.



  • @Pan-Jan said in How to replace a line, which has a specific text, with the content of a specific line?:

    It can be like that?

    You are welcome to submit your proposed solution but I strongly suggest you need to insert those regexes within special codes so the markdown interpreter (the window you type the response in) does not adjust any characters that you type.

    You will note that my regex was in red text. This can be achieved by using the ` (grave accent character) around the regex. This key is generally top left on a full US keyboard, on the same key as the tilde character (~).
    The OP supplied their updated version without the (\d+) groups just as text, much like you and it was affected.

    Mine:
    (?-s)^(((.+)?\R){5})(\d+)(.+\R)(?s)(.+?)(?-s)^((\d+).+"name": "noname".+\R)(?s)(.+)
    Theirs:
    (?-s)^(((.+)?\R){5})(.+\R)(?s)(.+?)(?-s)^(.+“name”: “noname”.+\R)(?s)(.+)
    

    Look closely at the quotes in each regex, notice any difference? This is one of many changes that can occur if you don’t add the codes around text you do NOT want altered.

    Also, once typing a response in the window, you have other commands available. You should see a </> button above the window, select this first when entering code text, note this is what I did above rather than use the grave character. It is also useful for when inserting example text so that others may copy the data “unchanged”.

    Terry



  • Nobody uses such quotation marks here

    “name”: “noname”
    

    Schowek01.jpg

    `red`
    

    red
    Red doesn’t work everywhere



  • @Pan-Jan

    It seems that you feel offended when someone gives you an well-intentioned advice.
    Why is that?
    If I copy your regex solution, I get, for example “name” instead of "name" and thus your solution is corrupted.
    If, as @TerryR recommends, you wrap your solution in ` or use a code tag, this will not happen.
    What is the problem here?



  • @Ekopalypse
    Don’t overdo it.
    I don’t feel offended at all.
    This is due to the short texts, but it is best to translate them.

    Only such quotation marks are correct

    "name": "noname"
    

    If I copy
    cudzys..jpg I’ll get
    Schowek02.jpg



  • @Pan-Jan said in How to replace a line, which has a specific text, with the content of a specific line?:

    Only such quotation marks are correct

    First, that’s false. It is perfectly reasonable for a question to include literal curly-quotes / smart-quotes in the question and/or answer.

    But moving on to your general thought, which seems to be “people usually don’t have text with curly quotes, so everyone – whether the person asking the question or the person answering – should somehow know that they need to mentally convert curly quotes into ASCII quotes”:

    let’s try out a couple scenarios. There will be three people in these scenarios: @QuestionAsker, @NoRedAnswer, and @YesRedAnswer.

    -----

    scenario 1, with @NoRedAnswer

    @QuestionAsker says:

    I have text, and want to strip out anything that’s inside quotes with no spaces:

    This will remove quotes: "NoSpaces"
    This will keep quotes: "This has spaces"
    

    After running search-and-replace, I want

    This will remove quotes: NoSpaces
    This will keep quotes: "This has spaces"
    

    @NoRedAnswer replies:

    Use the search term “(\w*)” and replace with $1

    @QuestionAsker says:

    Sorry, that didn’t work. I tried both (\w*) like you said, and tried “(\w*)”, but neither of them changed my text any.

    The @QuestionAsker didn’t understand the answer, because @NoRedAnswer didn’t format the answer using the forum’s formatting tools. This caused confusion and delay.

    scenario 2: same circumstance, but @YesRedAnswer

    @QuestionAsker says:

    I have text, and want to strip out anything that’s inside quotes with no spaces:

    This will remove quotes: "NoSpaces"
    This will keep quotes: "This has spaces"
    

    After running search-and-replace, I want

    This will remove quotes: NoSpaces
    This will keep quotes: "This has spaces"
    

    @YesRedAnswer replies:

    Use the search term "(\w*)" and replace with $1, with Search Mode set to regular expression

    @QuestionAsker says:

    Great, @YesRedAnswer! That worked on the first try! Thanks for your help. ++@YesRedAnswer

    -----

    As you should be able to see, the answer that did use red text for the regular expression was much more helpful, even though both people who answered came up with the same working regex.

    In the end, it’s your choice how to answer (or ask) questions in this forum. But you can choose to (1) make it easier for the others to understand you, or you can choose to (2) skip the red text and black text boxes, and make the person you’re helping (or the person you’re asking) try to figure out what you really meant, and probably get it wrong. We are just advising that you use style (1) rather than (2).



  • Got it … don’t put patterns in quotation marks

    Only such quotation marks are correct “”
    If necessary, use only such quotation marks “”

    But there is something I don’t understand.
    Maybe it’s the translation’s fault.

    “in quotation marks”

    I get it this way … you should remove the quotes:

    "ABC"
    

    and will be:

    ""
    


  • This post is deleted!

Log in to reply