Bash 搜索多个精确模式以替换为sed或awk

Bash 搜索多个精确模式以替换为sed或awk,bash,awk,sed,Bash,Awk,Sed,我有多个域名跟在主机名后面 abc-ddddd-guru.prod.gama1.com ddf-yy1.gama1.com ccd-dlf89-01.cdfgama1.com kk1-dlf88.gg1-gama1.com radaas.gama1.com 当我试图用sed在每个域名前添加ilo时。。事实并非如此 #sed的s/gama1.com//-ilo.gama1.com/g;s/cdfgama1.com/-ilo.cdfgama1.com/g';s/gg1-gama1.com/-ilo

我有多个域名跟在主机名后面

abc-ddddd-guru.prod.gama1.com
ddf-yy1.gama1.com
ccd-dlf89-01.cdfgama1.com
kk1-dlf88.gg1-gama1.com
radaas.gama1.com
当我试图用sed在每个域名前添加ilo时。。事实并非如此

#sed的s/gama1.com//-ilo.gama1.com/g;s/cdfgama1.com/-ilo.cdfgama1.com/g';s/gg1-gama1.com/-ilo.gg1-gama1.com/g';s/prod.gama1.com/-ilo.prod.gama1.com/g
'

作品:


请帮忙。。感谢

sed
解决方案:

sed -E 's/([^.]*|prod\.)?gama1.com$/-ilo.&/' file
  • ([^.]*|prod\)?
    -regex替换组,匹配除
    (由
    [^.]*
    表示)或
    prod.
    子域名以外的任何字符
    量词告诉我们在0到1倍之间匹配所提到的组

  • &
    -sed的重置值指向整个匹配


输出:

abc-ddddd-guru.-ilo.prod.gama1.com
ddf-yy1.-ilo.gama1.com
ccd-dlf89-01.-ilo.cdfgama1.com
kk1-dlf88.-ilo.gg1-gama1.com
radaas.-ilo.gama1.com

下面的
awk
可能会对您有所帮助

awk -v var="-ilo." '{match($0,/gama1.com/);print substr($0,1,RSTART-1) var substr($0,RSTART,RLENGTH)}'  Input_file
解释:现在也用解决方案添加解释

awk -v var="-ilo." '                                        ##Creating variable named var and have its value as -ilo. as per OP request.
{
  match($0,/gama1.com/);                                    ##Using match utility of awk by which we could use a regex and could match the specific needed part of a line. Its pattern is match(var/line,regex). If regex has a match on a line then out of the box variables named RSTART and RLENGTH will be having values in it. Where RSTART will have the starting value of matching regex and RLENGTH will have the length of matched regex by match utility.
  print substr($0,1,RSTART-1) var substr($0,RSTART,RLENGTH) ##Printing the substring here which starts from 1st letter to till the value of RSTART-1(means just before the match) print everything then print variable var here then again print substring of line whose value starts from RSTART to RLENGTH to print the exact match in line.
}
' Input_file                                                ##Mentioning the Input_file name here.
你可以试试这个

sed -E 's/([^.]*\.)(.*gama1.com$)/\1-ilo.\2/' infile
还是这个awk

awk '/gama1.com$/{sub(/\./,".-ilo.")}1' infile

对我来说,perl代码看起来不错:

perl -ne 'print "$1-ilo$2\n" if /^([\w-]+)(.+)/'

这里是一个帖子,不是怎么做,而是怎么做

路线是王道:

     's/gama1.com/-ilo.gama1.com/g; 
   s/cdfgama1.com/-ilo.cdfgama1.com/g; 
  s/gg1-gama1.com/-ilo.gg1-gama1.com/g; 
 s/prod.gama1.com/-ilo.prod.gama1.com/g'
我们看到普通图案垂直排列,可以将普通部分和不常见部分分开

's/(prod\.|gg1-|cdf)?gama1.com/-ilo.\1.gama1.com/g'
prod/gg1/cdf只是随机的例子吗?我不知道。也许它只是一组小写字符和数字。备选方案可以与OR:=|组合,但必须与parens分组,否则abc | def将匹配(abcef或abdef),而不是(abc或def)。随着paren的广泛使用,sed需要扩展正则表达式的-r开关,或者必须屏蔽paren:

's/(([a-z0-9]+\.)|([a-z0-9]+-)|([a-z0-9]+))?gama1.com/-ilo.\1.gama1.com/g'
这可以进一步压缩为字母,可以选择后跟点或负号:

 sed -r 's/(([a-z0-9]+)[.-]?)?gama1.com/-ilo.\2.gama1.com/g'

这是一个很好的答案。除一个域外,它在所有域上都有效。。ccd-ddf23-312.prod.-ilo.gama1.com。你能解释一下答案吗also@DasD,是的,请看我的解释。你为什么注意到它不适用于
ccd-ddf23-312.prod.-ilo.gama1.com
-这与你上一次的替换尝试
s/prod.gama1.com/-ilo.prod.gama1.com/g
?我不确定我在哪里遗漏了它,为什么它是失败的,但我得到的最后一个模式是像这个ccd-ddf23-prod.-ilo.gama1。com@DasD,那是因为你的问题模棱两可
 sed -r 's/(([a-z0-9]+)[.-]?)?gama1.com/-ilo.\2.gama1.com/g'