Find/Replace then delete spaces



  • I have to search for a 6 character number and then replace with a 10 character number. However, every column in the text file must be aligned. When I replace with the 10 character it pushes the rest of the line forward 4 characters. Is there any command where I can find/replace then delete the 4 spaces after the replacement?



  • A lot of guesswork at what you really mean. If this isn’t right, please read the FYI section and respond with more details

    Source Data:

    prefix  123         other text
    xiferp  12345       other text
    prefix  123456      other text
    xiferp  1234567     other text
    
    • Find what = (?-s)(\D{0,1})123456\h{4}
    • Replace with = 9876543210
    • Search Mode = regular expression

    End Result:

    prefix  123         other text
    xiferp  12345       other text
    prefix  9876543210  other text
    xiferp  1234567     other text
    

    Is this what you want?

    update: fixed the Find What – the original was an older version that wasn’t quite right…

    -----
    FYI:

    This forum is formatted using Markdown, with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end. It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it.
    If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.
    Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.



    • Replace with = ${1}9876543210

    Epic Fail

    posted the wrong regex and corrected. then saw I also posted the wrong replace. And I think it didn’t work, anyway.
    More testing, then I’ll post again



  • Let’s try again, with more test data

    Source Data:

    prefix          123         other text
    xiferp          12345       other text
    prefix          123456      other text
    nomatch         9123456     other text
    xiferp          1234567     other text
    123456          suffix
    9123456         xiffus
    1234567         suffix
    
    • Find what = (?-s)(\D|^\h*)123456\x20{4}
    • Replace with = ${1}9876543210
    • Search Mode = regular expression

    End Result:

    prefix          123         other text
    xiferp          12345       other text
    prefix          9876543210  other text
    nomatch         9123456     other text
    xiferp          1234567     other text
    9876543210      suffix
    9123456         xiffus
    1234567         suffix
    

    Details:

    • (?-s) = turn off .-matches-newline
    • (\D|^\h*) = grab and save as $1 (aka ${1} or \1): either a non-digit, or a start of the line that has zero or more whitespace before the rest of the match
    • 123456 = match the magic 6-digit number
    • \x20{4} = match exactly four space characters – need four spaces to allow room for four extra digits in the 10-digit replace; if there isn’t enough room, it won’t match this segment

    Replace:

    • ${0} = the matched expression above
    • 9876543210 = the magic 10-digit replacement number

    Okay, this time, I think it meets my self-definition.

    Is this what you want? If not, give a more explicit before and after. See the FYI section for info on how to format data like I did.



  • @PeterJones

    A lot of help, for maybe the wrong problem. Do you have a lot of spare time on your day job?



  • There are days when I have bursts of “start a physical process, wait 5-10min for it to finish (but have to be nearby in case something goes wrong), do some meaningful stuff that I cannot know until the process is finished, and repeat”. During those waits, it helps me think straight for the meaningful stuff if I occasionally try to do something that’s mentally completely different (but that’s easily interruptible) during the wait time. Hence, my presence on this forum. :-)



  • @PeterJones

    There are days when I have bursts of "start a physical process, wait 5-10min for it to finish … do something that’s mentally completely different (but that’s easily interruptible)

    same same, i suppose many of us, usually have quite a bit to handle, but either it’s impossible to focus on a specific fraction of it within the given time frame, or a short break is needed, doing something else … something mentally challenging, yet nothing to depend on a single individual.

    alt

    … and maybe, just maybe, i’ve got some tendencies to procrastinate every once in a while 😁 …


    ps:

    please read the FYI section

    produces a positiv smile every time it’s seen 😉
    (rest assured, that i’d ask you before using, committing or reposting it)



  • I started that boilerplate because if you just link to the FAQ’s, people tend to ignore the links, but if it’s got some context, you might convince a couple people to actually follow through and read the FAQ(s). But I got tired of rephrasing the same thing every time, so made up my boilerplate to paste in. Feel free to use it – it’s basically a pastable FAQ.


Log in to reply