Q: How to delete a certain word from every line that contains another certain word



  • Hello,

    After searching the internet I didn’t find any thing that would help me.

    I want to delete a certain word from every line that contains another certain word.

    Example:

    human animal thing
    car human thing
    cat human animal
    car bike road
    bike ship human

    I want to delete human when the line contains thing =>>

    animal thing
    car thing
    cat human animal
    car bike road
    bike ship human

    I have thousand of lines

    Thanks in advance



  • @Ziad-Aborami
    First question, is the word human always going to appear before thing? Or could the 2 words be on the line in any order?

    Terry



  • The could appear in any order



  • @Ziad-Aborami
    I have a solution. Not sure how familiar you are with regular expressions (regexes) but these are like a filter using ‘wildcards’ to find text that may be similar but not exactly like something else.

    Anyways, under the replace menu option (Search, replace), make sure the search mode is set for ‘regular expression’ and have wrap-around ticked.

    Find What:(?-s)((.*thing.*) human(.*))|((.*)human (.*thing.*))
    Replace With:(?1\2\3)(?4\5\6)

    The (?-s) forces the regex to work 1 line at a time
    Next we have the 2 options divided by a | which means OR. This allows us to cater for which word occurs first.
    The replace field checks for which option was triggered in the find what field. We then put back the relevant text without the word human.
    Note with the text human there is a space before the word in one option and behind it in the other. This will remove 1 space so that the resulting line does NOT have 2 spaces beside each other. On the small test I did it worked. However it is always best to work on a copy of your data until you are sure that regex is working as expected.

    Terry



  • @Ziad-Aborami
    After posting my answer I thought that the word you are searching for may be contained within another word. So for example if I wanted to search for cat and dog then the following line is OK
    the cat followed the dog
    however I would NOT want the following line as the word is not distinct
    the catch of the day was a big dogfish
    Therefore if this is the case a more correct Find What field would be:
    (?-s)((.*\bthing\b.*) \bhuman\b(.*))|((.*)\bhuman\b (.*\bthing\b.*))

    It uses the \b boundary metacharacter to define the word and thus exclude situations where it’s part of a bigger word.

    Terry



  • Thanks very much
    I have tried the first one and it does work

    Best wishes 😊


Log in to reply