如果一个单词也位于下一个单词(sed)中,该如何从一个单词中删除呢? [英] How to remove characters from a word if they are also in the next word (sed)?
本文介绍了如果一个单词也位于下一个单词(sed)中,该如何从一个单词中删除呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种方法来删除第一个单词中的所有字符,前提是该字符位于第二个单词中.输入看起来像这样:
I'm trying to find a way to delete all characters in the first word IF that character is in the second word. The input looks like this:
计算机费用
结果应为:"mpuer",因为删除了c,o和t.像这样的多行之间用回车符分隔,两个单词之间用空格分隔.
And the result should be: "mpuer" because the c, o and t were deleted. There are multiple lines like this separated by a return, the 2 words are separated by a space.
我一直在寻找解决方案的时间,但我真的被困住了.感谢所有帮助.
I've been searching quite some time for the solution, but I'm really stuck. All help is appreciated.
推荐答案
这可能对您有用:
echo "computer cost" |
sed ':a;s/\(.\)\(.* .*\1.*\)/\2/;ta;s/ .*//'
mpuer
说明:
- 为将来的分支命令
:a;
添加标签 - 删除第一个单词中与第二个单词中相同字符匹配的字符
s/\(.\)\(.*.* \ 1.* \)/\ 2/
- 如果发生替换,则分支到标签
ta
- 如果没有其他替换,请删除第二个单词.
s/.*//
替换正则表达式可以进一步解释:
The substitution regexp can be further explained:
-
\(.\)
与单词word中的任何字符匹配(后来称为\ 1
) -
\(.*.* \ 1.* \)
匹配单词.*
的其余部分中的所有字符,后跟空格.*
中的所有字符,然后是来自第一个\ 1
的匹配字符,然后是来自第二个的其余字符.*
此分组以后称为\ 2
. - 如果以上匹配项替换为
\ 2
,从而有效删除了匹配字符\ 1
\(.\)
matches any character in word one (later refered to as\1
)\(.* .*\1.*\)
matches any characters in the remainder of a word one.*
followed by a spacefollowed by some on none characters in word two
.*
followed by a matching character from word one\1
followed by the remaining characters from word two.*
this grouping will later be known as\2
.- If the above matches replace it by
\2
thus effectively deleting the matching character\1
这篇关于如果一个单词也位于下一个单词(sed)中,该如何从一个单词中删除呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文