Linuxのファイルで重複文字列を一意にする

Linuxのファイルで重複文字列を一意にする

次のようなproba.ldifファイルがあります。

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0015f5e3d05d4d52b0cb85db69474db3,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220303153032Z
modifyTimestamp: 20220303153032Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId: 1
EpsIndContextId: 2

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0040fb1140104f9fbc4be38be3db5965,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220301120221Z
modifyTimestamp: 20220301120221Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId: 1
EpsIndContextId: 5
EpsIndContextId: 15

各dnに一意のEpsIndContextIdを作成し、最後に数字を追加し、次のファイルを取得したいと思います。

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0015f5e3d05d4d52b0cb85db69474db3,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220303153032Z
modifyTimestamp: 20220303153032Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId1: 1
EpsIndContextId2: 2

dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=0040fb1140104f9fbc4be38be3db5965,ou=multiSCs,dc=three
structuralObjectClass: EpsStaticInf
objectClass: EpsStaticInf
entryDS: 1
nodeId: 21
createTimestamp: 20220301120221Z
modifyTimestamp: 20220301120221Z
EpsStaInfId: EpsStaInf
EpsProfileId: 10
EpsOdb: 0
EpsRoamAllow: TRUE
CDC: 1
EpsIndDefContextId: 1
EpsIndAmbrMaxUl: 320000000
EpsIndAmbrMaxDl: 1024000000
EpsRoamRestrict: TRUE
EpsTenantId: 1
EpsIndContextId1: 1
EpsIndContextId2: 5
EpsIndContextId3: 15

どうすればいいですか?

ベストアンサー1

そしてperl

perl -pe '$i = 0 if /^dn:/; s/^EpsIndContextId\K/++$i/e' < prueba.ldif

または、ファイルをin-place編集します。

perl -i -pe '$i = 0 if /^dn:/; s/^EpsIndContextId\K/++$i/e' prueba.ldif

dn:上記では、で始まる行に会うたびにカウンタをリセットします。代わりに、空行をif /^dn:/検索したり、空白行(空白文字のみで構成された行)を検索したり、@glennjackmanが提案したように、行の代わりにレコードが1つ以上の空白行(2つ以上)で区切られている段落モードを使用できます。改行)、トピックの先頭と各レコードの項目を置き換えるフラグの代わりに、トピック(段落)の各行の先頭と一致するように代替フラグを使用します。if /^$/unless /\S/-00m^g

perl -00 -pe '$i = 0; s/^EpsIndContextId\K/++$i/emg' < prueba.ldif

おすすめ記事