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 humanI want to delete human when the line contains thing =>>
animal thing
car thing
cat human animal
car bike road
bike ship humanI 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 meansOR
. 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 wordhuman
.
Note with the texthuman
there is aspace
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 forcat
anddog
then the following line is OK
thecat
followed thedog
however I would NOT want the following line as the word is not distinct
thecat
ch of the day was a bigdog
fish
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 workBest wishes 😊