1

I have a file prueba.ldif like this:

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

I want to make unique the EpsIndContextId for each dn, adding a number to the end, getting as a result a file like this:

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

How can I do that?

3 Answers 3

2

With perl:

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

Or editing the file in-place:

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

Above, we reset the counter whenever a line starting with dn: is encountered. You could change if /^dn:/ to if /^$/ to search for empty lines instead or unless /\S/ to search for blank lines (lines made up of only whitespace characters), or as @glennjackman suggests, use the paragraph mode with -00 where records, instead of being lines, are delimited by sequences of one or more empty lines (2 or more newline characters), and use the m flag in the substitution for ^ to match at the start of each line in the subject (paragraph) instead of only at the start of the subject and the g flag to substitute every occurrence in the record:

perl -00 -pe '$i = 0; s/^EpsIndContextId\K/++$i/emg' < prueba.ldif
3
  • Similar, but reads the file a "paragraph" at a time: perl -00 -pe '$i = 0; s/^EpsIndContextId\K/++$i/meg' Mar 16 at 19:35
  • @glennjackman, thanks, I've incorporated that in the answer. Mar 17 at 7:47
  • Thanks a lot to both of you. Not only for the solution, but for the explanation so I can actually understand how the solution works instead of just copying and paste :) Mar 21 at 8:21
0

Here's a sed solution:

sed -E 's/(^EpsIndContextId)(:) (.*$)/\1\3\2 \3/' prueba.ldif

1
  • Thank you very much, I was looking for a solution with sed and this one works perfectly!!! Mar 21 at 8:19
0

POSIX awk:

awk '/^dn: /{c=0} /^EpsIndContextId/{sub(/^EpsIndContextId/, "&"++c)} 1' test

Reset the counter (c) ad the beginning of each section (dn), so each time it finds an EpsIndContextId it increment the counter and add it to this.

Or as , @Ed Morton suggests :

awk '/^dn: /{c=0} sub(/^EpsIndContextId/, "&"c+1){c++} 1' test

to avoid using the same regexp twice.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.