使用awk在数据块中存在多个现有字符串

时间:2016-05-09 14:27:05

标签: unix awk

我希望所有dn(如dn:uid = srinivas)使用任何unix命令都有多个sn:。我的文件内容是:

dn: uid=srinivas
sn: srinivas
sn: srinivas-xxx
sn: xxxxxxxxxxx
cn: srinivas rao g
givenname: srinivas

dn: uid=geetha
sn: geeth
cn: geetha b
givenname: geetha

dn: uid=bhuvana
sn: bhuvana
sn: bhuvana-xxx
cn: xxxxxx
givenname: xxxxxxxxx

对于上述文件内容,我的输出应为:

dn: uid=srinivas
dn: uid=bhuvana

2 个答案:

答案 0 :(得分:0)

$ awk -v RS= -F'\n' 'gsub(/\nsn:/,"&")>1{print $1}' file
dn: uid=srinivas
dn: uid=bhuvana

答案 1 :(得分:0)

另一个awk

$ awk '/^dn:/{d=$0;c=0} d&&/^sn:/&&2==++c{print d;d=""} ' file
dn: uid=srinivas
dn: uid=bhuvana