2017-10-21 85 views
1

我的问题是在sed的帮助下每出现一次包含子字符串“two”的单词,但不要加倍单词“两节”本身sed命令将每个出现的包含子字符串但不是双字本身的单词翻倍的次数

我迄今为止尝试:

cat filename | sed 's/ \ (two\) /\1\1/g' 

但我的问题是,它是重复的词本身。

例如:

two times twothird threetwothree 

执行上述sed的操作是什么我之后是:

twotwo times twotwothird threetwotwothree 

但我打算到这里是:

two times twothird twothird threetwothree threetwothree 

任何人有任何想法如何解决这个问题?

+0

它的/ \(二但stackoverflow只是显示/(由于某种原因 – Lucifellow

回答

2

使用此:

sed -E 's/([[:alpha:]]*two[[:alpha:]]+|[[:alpha:]]+two[[:alpha:]]*)/\1 \1/g' file 
  • [[:alpha:]]*two[[:alpha:]]+比赛有在开头或中间有two所有的字,但不two本身
  • [[:alpha:]]+two[[:alpha:]]*比赛,在中间或末尾有两个所有单词,但不是two本身
  • ()表达式提取替换表达式中使用的匹配词\1 \1

实施例:

s="two times twothird two threetwothree onetwo twothree two" 
sed -E 's/([[:alpha:]]*two[[:alpha:]]+|[[:alpha:]]+two[[:alpha:]]*)/\1 \1/g' <<< "$s" 

产生

two times twothird twothird two threetwothree threetwothree onetwo onetwo twothree twothree two 
+0

有一个问题,我以前答复呃不会处理以'two'结尾的单词。现在更正。 – codeforester

1
sed -r 's/(\S*two\S+|\S+two\S*)/\1 \1/g 

检查用于至少一个前缀或后缀 “两个”

相关问题