Search for word and if found move it up one row



  • I am needing to perform a search on a large txt file for "** NEW ** and if found place it in the row above. I also don’t want to add or remove spaces on the lines. Not sure how to describe it but if I remove the found 9 characters ** NEW ** I would like to replace those with spaces. Similarly, when the data is moved up one row, I would then need to trim off 9 spaces.

    Thanks



  • @George-Corder

    Not sure how to describe it

    By far the easiest way for help to be provided is if you show some sample before and after data. Be sure to use this button so that the site doesn’t mess with the formatting of your data:

    c3484961-8d10-479a-8064-3d395649f7af-image.png



  • This post is deleted!


  • @Alan-Kilborn
    Sorry, the code didn’t appear as I expected so I uploaded a screenshot.

    Notepad.png



  • @George-Corder

    didn’t appear as I expected so I uploaded a screenshot.

    A screenshot is not as desirable for those that might want to play with your data in order to offer a solution. No one wants to type something in in order to experiment.
    Not sure what might have gone wrong for you in entering the data, but a lot of people seem to have trouble providing data.

    Regarding your problem, it’s tough to say as your sample is all the same data.
    I would have been great if instead of providing 5 identical lines, you provided 5 different lines…

    I also don’t want to add or remove spaces on the lines.

    Well, you’re adding spaces before the ** NEW ** on the line, are there spaces after it?

    I don’t know…it still seems inconclusive to me…as a problem statement…



  • Hello @george-corder, @alan-kilborn and All,

    Given your text, below :

    Iaculis at erat pellentesque adipiscing ** NEW ** commodo. Orci phasellus egestas tellus rutrum
    Iaculis at erat pellentesque adipiscing ** NEW ** commodo. Orci phasellus egestas tellus rutrum
    
    Iaculis at erat pellentesque adipiscing ** NEW ** commodo. Orci phasellus egestas tellus rutrum
    
    Iaculis at erat pellentesque adipiscing ** NEW ** commodo. Orci phasellus egestas tellus rutrum
    
    Iaculis at erat pellentesque adipiscing ** NEW ** commodo. Orci phasellus egestas tellus rutrum
    

    it seems that the ** NEW ** string always comes after 40 space characters

    So using the regex S/R, below :

    SEARCH  =(?-i)(^\R)?^(.+)\Q** NEW **
    
    REPLACE =                                        ** NEW **\r\n\2         
    

    This text would be changed as :

                                            ** NEW **
    Iaculis at erat pellentesque adipiscing           commodo. Orci phasellus egestas tellus rutrum
                                            ** NEW **
    Iaculis at erat pellentesque adipiscing           commodo. Orci phasellus egestas tellus rutrum
                                            ** NEW **
    Iaculis at erat pellentesque adipiscing           commodo. Orci phasellus egestas tellus rutrum
                                            ** NEW **
    Iaculis at erat pellentesque adipiscing           commodo. Orci phasellus egestas tellus rutrum
                                            ** NEW **
    Iaculis at erat pellentesque adipiscing           commodo. Orci phasellus egestas tellus rutrum
    

    Notes :

    • Of course, select the Regular expression search mode

    • The search zone begins right after the = sign

    • The replace field :

      • Begins right after the = sign, with 40 space characters

      • Ends with 9 space characters ( length of the string ** NEW ** )

    Best Regards,

    guy038



  • @George-Corder said in Search for word and if found move it up one row:

    I am needing to perform a search on a large txt file for "** NEW ** and if found place it in the row above.

    As @Alan-Kilborn says it’s tough to provide a solution as your sample data is all the same. However I have considered an idea that works regardless of the length of any line containing the ** NEW ** string (so lines can be of varying length). An actual solution could be presented once you have confirmed answers to my questions below and hopefully also some more representative example data. Otherwise you have my idea to work with and possibly that can lead in the right direction to solve your need.

    So the idea is:

    1. For each line with the string, duplicate it above the current line together with a “flag” at the end (say #1). This flag denotes it as the new line which will be further processed to remove the characters, replacing them with spaces.
    2. The “duplicate” line will now have the characters before the ** NEW ** string replaced with spaces, 1 character at a time since we don’t know how many there are. The regex (regular expression) to perform this will only need running once with the “Replace All” button.
    3. The remaining characters on the “duplicate” line after the ** NEW ** string will be removed, although if required they could also be replaced with spaces.
    4. The ‘flag’ (e.g. #1) will be removed on the “duplicate” line along with the ** NEW ** string on the original line with spaces replacing the ** NEW ** string to keep length correct.

    Questions which need answering:

    1. Is the “duplicate” line created actually an inserted line, or is it replacing an existing line?
    2. Are spaces needed after the ** NEW ** string on the “duplicate” line or can this line end at the ** NEW ** string, so it’s length is different from it’s original which is the next line below?

    So I have outlined the idea in 4 steps. They might possibly be combined (especially #2, #3 and #4) although complexity grows fast and for a one off job I would likely stay with the 4 steps as presented being separate regexes.

    Terry



  • @guy038 said in Search for word and if found move it up one row:

    …with 40 space characters

    My bet is that it isn’t a constant 40 spaces that is wanted.
    It will depend upon the other lines and where ** NEW ** occurs in them.
    However, and it’s a big however, we need more data to be able to help!


Log in to reply