如何在正则表达式中允许\ b进行sed命令替换

时间:2015-06-12 18:12:23

标签: regex bash sed

我有一个包含

的文本文件 temp.txt
Hello World
My name is MayukhSarkar
My name is mayukh
My name is MAYUKH
My name is MaYuKh
My name is mAyUkH

sed命令

sed 's/\b[M,m][A,a][Y,y][U,u][K,k][H,h]\b/sobo/g' temp.txt

并未将Mayukh的出现替换为sobo

但没有\b的sed命令正在运行

sed 's/[M,m][A,a][Y,y][U,u][K,k][H,h]/sobo/g' temp.txt

但它也将MayukhSarkar替换为soboSarkar

2 个答案:

答案 0 :(得分:4)

$ sed -r 's/(^|[^[:alnum:]])[Mm][Aa][Yy][Uu][Kk][Hh]([^[:alnum:]]|$)/\1sobo\2/' file
Hello World
My name is MayukhSarkar
My name is sobo
My name is sobo
My name is sobo
My name is sobo

或使用Bedrock sed,您似乎正在使用:

sed 's/\(^\|[^[:alnum:]]\)[Mm][Aa][Yy][Uu][Kk][Hh]\([^[:alnum:]]\|$\)/\1sobo\2/'
 file

使用GNU sed:

$ sed 's/\bmayukh\b/sobo/i' file                   
Hello World
My name is MayukhSarkar
My name is sobo
My name is sobo
My name is sobo
My name is sobo

使用GNU awk:

$ awk 'BEGIN{IGNORECASE=1} {sub(/\<mayukh\>/,"sobo")} 1' file                
Hello World
My name is MayukhSarkar
My name is sobo
My name is sobo
My name is sobo
My name is sobo

任何awk:

$ awk 'match(tolower($0),/(^|[[:space:]])mayukh([[:space:]]|$)/) { tgt = substr($0,RSTART,RLENGTH); sub(/[^[:space:]]+/,"sobo",tgt); $0 = substr($0,1,RSTART-1) tgt substr($0,RSTART+RLENGTH)} 1' file
Hello World
My name is MayukhSarkar
My name is sobo
My name is sobo
My name is sobo
My name is sobo

答案 1 :(得分:1)

使用sed应该是最好的:

sed -E 's/(^| )[Mm][Aa][Yy][Uu][Kk][Hh]( |[^S]|$)/\1sobo\2/g' temp.txt

OS X中,-E选项会将正则表达式解释为扩展(现代)正则表达式(ERE)而不是基本正则表达式(BRE)。在Linux中,这将是-r

awkgsub一起使用,替换特定的不区分大小写的单词非常简单:

awk '{gsub(/(^| )[Mm][Aa][Yy][Uu][Kk][Hh]( |$)/," sobo ");print}' temp.txt

这适用于所有平台OS XLinux

编辑:如果要替换的单词恰好位于开头或结尾或者行,那么您需要稍微复杂一点:

awk '{gsub(/(^| )[Mm][Aa][Yy][Uu][Kk][Hh]( |$)/,"-sobo-");gsub(/^-sobo-/,"sobo ");gsub (/-sobo-$/," sobo");gsub (/-sobo-/," sobo "); print}'

这基本上可以使被替换的单词前面或后面没有空格。

<强>结果:

My name is MayukhSarkar
My name is sobo 
My name is sobo 
My name is sobo 
My name is sobo