search for a pattern and always print the first line which contains the cn

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I've a file which has the following output:
The dn: can can have more rdcPositions.
I only need the dn: which has an rdcPositions contains acme#6#
The result should print the cn and also the rdcPosition
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946
702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</ne
wstatus><date>1529318977</date></change><change><date>1529319116</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>15
29481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=922445,ou=Entities,ou=Active,ou=Vault,o=acme#5#<position><cn>42
79084890</cn><reqdate>1429014997</reqdate><startdate>1429014997</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1429014997</date></change><change><date>1429023084</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1525107741</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change><change><date>1525126716</date><previousstatus>6</previousstatus><news
tatus>5</newstatus></change></lifecycle></position>
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>97
26910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1528120494</date></change><change><date>1528123478</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>20
68839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1406284665</date></change><change><date>1406284666</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>76
03071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1400325753</date></change><change><date>1400325754</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802
042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</ne
wstatus><date>1406284761</date></change><change><date>1406284762</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>14
49224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
rdcPosition: cn=312936,ou=Entities,ou=Active,ou=Vault,o=acme#3#<position><cn>19
23461515</cn><reqdate>1449217172</reqdate><startdate>1449217172</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1449217172</date></change><change><date>1449225081</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
text-processing ldap
add a comment |Â
up vote
0
down vote
favorite
I've a file which has the following output:
The dn: can can have more rdcPositions.
I only need the dn: which has an rdcPositions contains acme#6#
The result should print the cn and also the rdcPosition
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946
702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</ne
wstatus><date>1529318977</date></change><change><date>1529319116</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>15
29481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=922445,ou=Entities,ou=Active,ou=Vault,o=acme#5#<position><cn>42
79084890</cn><reqdate>1429014997</reqdate><startdate>1429014997</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1429014997</date></change><change><date>1429023084</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1525107741</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change><change><date>1525126716</date><previousstatus>6</previousstatus><news
tatus>5</newstatus></change></lifecycle></position>
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>97
26910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1528120494</date></change><change><date>1528123478</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>20
68839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1406284665</date></change><change><date>1406284666</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>76
03071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1400325753</date></change><change><date>1400325754</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802
042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</ne
wstatus><date>1406284761</date></change><change><date>1406284762</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>14
49224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
rdcPosition: cn=312936,ou=Entities,ou=Active,ou=Vault,o=acme#3#<position><cn>19
23461515</cn><reqdate>1449217172</reqdate><startdate>1449217172</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1449217172</date></change><change><date>1449225081</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
text-processing ldap
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've a file which has the following output:
The dn: can can have more rdcPositions.
I only need the dn: which has an rdcPositions contains acme#6#
The result should print the cn and also the rdcPosition
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946
702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</ne
wstatus><date>1529318977</date></change><change><date>1529319116</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>15
29481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=922445,ou=Entities,ou=Active,ou=Vault,o=acme#5#<position><cn>42
79084890</cn><reqdate>1429014997</reqdate><startdate>1429014997</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1429014997</date></change><change><date>1429023084</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1525107741</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change><change><date>1525126716</date><previousstatus>6</previousstatus><news
tatus>5</newstatus></change></lifecycle></position>
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>97
26910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1528120494</date></change><change><date>1528123478</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>20
68839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1406284665</date></change><change><date>1406284666</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>76
03071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1400325753</date></change><change><date>1400325754</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802
042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</ne
wstatus><date>1406284761</date></change><change><date>1406284762</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>14
49224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
rdcPosition: cn=312936,ou=Entities,ou=Active,ou=Vault,o=acme#3#<position><cn>19
23461515</cn><reqdate>1449217172</reqdate><startdate>1449217172</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1449217172</date></change><change><date>1449225081</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
text-processing ldap
I've a file which has the following output:
The dn: can can have more rdcPositions.
I only need the dn: which has an rdcPositions contains acme#6#
The result should print the cn and also the rdcPosition
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946
702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</ne
wstatus><date>1529318977</date></change><change><date>1529319116</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>15
29481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=922445,ou=Entities,ou=Active,ou=Vault,o=acme#5#<position><cn>42
79084890</cn><reqdate>1429014997</reqdate><startdate>1429014997</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1429014997</date></change><change><date>1429023084</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1525107741</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change><change><date>1525126716</date><previousstatus>6</previousstatus><news
tatus>5</newstatus></change></lifecycle></position>
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>97
26910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1528120494</date></change><change><date>1528123478</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,o
u=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>20
68839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1406284665</date></change><change><date>1406284666</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>76
03071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</
newstatus><date>1400325753</date></change><change><date>1400325754</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>
1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></
change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802
042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><end
date>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</ne
wstatus><date>1406284761</date></change><change><date>1406284762</date><previ
ousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>14
49224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></ch
ange></lifecycle></position>
rdcPosition: cn=312936,ou=Entities,ou=Active,ou=Vault,o=acme#3#<position><cn>19
23461515</cn><reqdate>1449217172</reqdate><startdate>1449217172</startdate><e
nddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</
newstatus><date>1449217172</date></change><change><date>1449225081</date><pre
viousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></
position>
text-processing ldap
edited yesterday
Jeff Schaller
30.7k846104
30.7k846104
asked yesterday
user303833
1
1
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago
add a comment |Â
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
The input seems to be LDIF as specified in RFC 2849.
I'd strongly recommend not to use the usual awk/sed/grep tool chain for processing LDIF because of the following reasons:
- Long attribute values lines (including dn:) are wrapped with a single space indicating the line-continuation.
- Attribute values containing non-ASCII chars will be base64-encoded.
The best solution is to use a decent LDIF parser for your favorite scripting language.
E.g. for Python use the module ldif in python-ldap:
See docs: ldif -- LDIF parser and generator
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
add a comment |Â
up vote
0
down vote
Your desired output is not quite clear. How far would this get you:
awk '
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
For your changed request in your comment, how far would this get you? If not satisfied, please become way more specific defining your desired output.
awk '
DN = $1 FS $2
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print DN, substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
add a comment |Â
up vote
0
down vote
Using the following sed script (it assumes we're running it with sed -n):
/^dn:/ # this is a "dn" line
N; # append the next line
s/n //; # remove the newline and the space
x; # exchange pattern space with hold space
/o=acme#6#/p; # print if pattern space contains our string
d; # delete from pattern space, start next cycle
/^rdcPosition:/ # this is a "rdcPosition" line
:again; # define label for loop
N; # append the next line
s/n //; # remove the newline and the space
#</position>#!b again; # if the end tag "</position>" was not read, loop
/o=acme#6#/H; # append to hold space if matching what we're looking for
$ # at the very end of input
x; # exchange pattern and hold space
/o=acme#6#/p; # print if pattern space contains our string
What the script does is essentially to build up a string in the sed "hold space" (general purpose buffer that survives between cycles). The string will start with the dn line and then it will be appended with the rdcPosition lines that contain the particular string that we are interested in.
Whenever a new dn line is found, or when we are at the end of the input, the hold space is conditionally printed if it contains our string (it may not contain it if none of the rdcPosition lines for the current dn line matched).
Testing it:
$ sed -n -f script.sed file
dn: cn=00fa69bd-bede-4918-a017b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1529318977</date></change><change><date>1529319116</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1529481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>9726910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1528120494</date></change><change><date>1528123478</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2068839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284665</date></change><change><date>1406284666</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7603071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1400325753</date></change><change><date>1400325754</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284761</date></change><change><date>1406284762</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
A logically equivalent awk script which produces the same output as the sed code above:
/^dn:/
if (hold ~ "o=acme#6#")
print hold
hold = $0;
getline
hold = hold substr($0, 2)
next
/^rdcPosition:/
line = $0
while (line !~ "</position>")
getline
line = line substr($0, 2)
if (line ~ "o=acme#6#")
hold = hold ORS line
END
if (hold ~ "o=acme#6#")
print hold
The substr($0, 2) calls will strip the leading space off of the broken lines in the input.
Both scripts assumes that the dn line is broken into exactly two lines.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The input seems to be LDIF as specified in RFC 2849.
I'd strongly recommend not to use the usual awk/sed/grep tool chain for processing LDIF because of the following reasons:
- Long attribute values lines (including dn:) are wrapped with a single space indicating the line-continuation.
- Attribute values containing non-ASCII chars will be base64-encoded.
The best solution is to use a decent LDIF parser for your favorite scripting language.
E.g. for Python use the module ldif in python-ldap:
See docs: ldif -- LDIF parser and generator
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
add a comment |Â
up vote
1
down vote
The input seems to be LDIF as specified in RFC 2849.
I'd strongly recommend not to use the usual awk/sed/grep tool chain for processing LDIF because of the following reasons:
- Long attribute values lines (including dn:) are wrapped with a single space indicating the line-continuation.
- Attribute values containing non-ASCII chars will be base64-encoded.
The best solution is to use a decent LDIF parser for your favorite scripting language.
E.g. for Python use the module ldif in python-ldap:
See docs: ldif -- LDIF parser and generator
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The input seems to be LDIF as specified in RFC 2849.
I'd strongly recommend not to use the usual awk/sed/grep tool chain for processing LDIF because of the following reasons:
- Long attribute values lines (including dn:) are wrapped with a single space indicating the line-continuation.
- Attribute values containing non-ASCII chars will be base64-encoded.
The best solution is to use a decent LDIF parser for your favorite scripting language.
E.g. for Python use the module ldif in python-ldap:
See docs: ldif -- LDIF parser and generator
The input seems to be LDIF as specified in RFC 2849.
I'd strongly recommend not to use the usual awk/sed/grep tool chain for processing LDIF because of the following reasons:
- Long attribute values lines (including dn:) are wrapped with a single space indicating the line-continuation.
- Attribute values containing non-ASCII chars will be base64-encoded.
The best solution is to use a decent LDIF parser for your favorite scripting language.
E.g. for Python use the module ldif in python-ldap:
See docs: ldif -- LDIF parser and generator
answered 9 hours ago
Michael Ströder
2296
2296
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
add a comment |Â
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Would you be able to provide a solution that actually parses the given data for the string?
â Kusalananda
8 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Yes, I could do that. But it would also take me half an hour. So please read the docs: LDIF parser example
â Michael Ströder
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
Then maybe you should have written a comment instead?
â Kusalananda
7 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I could delete my answer. No problem. But bear in mind that it's also a bad habit to ask others to do your complete home-work.
â Michael Ströder
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
I don't think you should delete the answer (I upvoted it). It is the correct answer, but it's also only half an answer.
â Kusalananda
6 hours ago
add a comment |Â
up vote
0
down vote
Your desired output is not quite clear. How far would this get you:
awk '
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
For your changed request in your comment, how far would this get you? If not satisfied, please become way more specific defining your desired output.
awk '
DN = $1 FS $2
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print DN, substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
add a comment |Â
up vote
0
down vote
Your desired output is not quite clear. How far would this get you:
awk '
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
For your changed request in your comment, how far would this get you? If not satisfied, please become way more specific defining your desired output.
awk '
DN = $1 FS $2
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print DN, substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your desired output is not quite clear. How far would this get you:
awk '
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
For your changed request in your comment, how far would this get you? If not satisfied, please become way more specific defining your desired output.
awk '
DN = $1 FS $2
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print DN, substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
Your desired output is not quite clear. How far would this get you:
awk '
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
For your changed request in your comment, how far would this get you? If not satisfied, please become way more specific defining your desired output.
awk '
DN = $1 FS $2
while (match($0, /rdcPosition: [^ ]*acme#6#[^ ]*/)) print DN, substr ($0, RSTART, RLENGTH)
$0 = substr ($0, RSTART + RLENGTH);
' file
dn: cn=00fa69bd-bede-4918-a017-b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#8946
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#97
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#20
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#76
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#2802
edited 13 hours ago
answered yesterday
RudiC
612
612
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
add a comment |Â
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
I need the dn: cn= as well with the output
â user303833
18 hours ago
I need the dn: cn= as well with the output
â user303833
18 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
what do I wrong? I get other output. rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946 rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946/n
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
the dn: is not printed ?
â user303833
10 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
Are you using the sample data from post#1?
â RudiC
9 hours ago
add a comment |Â
up vote
0
down vote
Using the following sed script (it assumes we're running it with sed -n):
/^dn:/ # this is a "dn" line
N; # append the next line
s/n //; # remove the newline and the space
x; # exchange pattern space with hold space
/o=acme#6#/p; # print if pattern space contains our string
d; # delete from pattern space, start next cycle
/^rdcPosition:/ # this is a "rdcPosition" line
:again; # define label for loop
N; # append the next line
s/n //; # remove the newline and the space
#</position>#!b again; # if the end tag "</position>" was not read, loop
/o=acme#6#/H; # append to hold space if matching what we're looking for
$ # at the very end of input
x; # exchange pattern and hold space
/o=acme#6#/p; # print if pattern space contains our string
What the script does is essentially to build up a string in the sed "hold space" (general purpose buffer that survives between cycles). The string will start with the dn line and then it will be appended with the rdcPosition lines that contain the particular string that we are interested in.
Whenever a new dn line is found, or when we are at the end of the input, the hold space is conditionally printed if it contains our string (it may not contain it if none of the rdcPosition lines for the current dn line matched).
Testing it:
$ sed -n -f script.sed file
dn: cn=00fa69bd-bede-4918-a017b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1529318977</date></change><change><date>1529319116</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1529481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>9726910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1528120494</date></change><change><date>1528123478</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2068839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284665</date></change><change><date>1406284666</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7603071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1400325753</date></change><change><date>1400325754</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284761</date></change><change><date>1406284762</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
A logically equivalent awk script which produces the same output as the sed code above:
/^dn:/
if (hold ~ "o=acme#6#")
print hold
hold = $0;
getline
hold = hold substr($0, 2)
next
/^rdcPosition:/
line = $0
while (line !~ "</position>")
getline
line = line substr($0, 2)
if (line ~ "o=acme#6#")
hold = hold ORS line
END
if (hold ~ "o=acme#6#")
print hold
The substr($0, 2) calls will strip the leading space off of the broken lines in the input.
Both scripts assumes that the dn line is broken into exactly two lines.
add a comment |Â
up vote
0
down vote
Using the following sed script (it assumes we're running it with sed -n):
/^dn:/ # this is a "dn" line
N; # append the next line
s/n //; # remove the newline and the space
x; # exchange pattern space with hold space
/o=acme#6#/p; # print if pattern space contains our string
d; # delete from pattern space, start next cycle
/^rdcPosition:/ # this is a "rdcPosition" line
:again; # define label for loop
N; # append the next line
s/n //; # remove the newline and the space
#</position>#!b again; # if the end tag "</position>" was not read, loop
/o=acme#6#/H; # append to hold space if matching what we're looking for
$ # at the very end of input
x; # exchange pattern and hold space
/o=acme#6#/p; # print if pattern space contains our string
What the script does is essentially to build up a string in the sed "hold space" (general purpose buffer that survives between cycles). The string will start with the dn line and then it will be appended with the rdcPosition lines that contain the particular string that we are interested in.
Whenever a new dn line is found, or when we are at the end of the input, the hold space is conditionally printed if it contains our string (it may not contain it if none of the rdcPosition lines for the current dn line matched).
Testing it:
$ sed -n -f script.sed file
dn: cn=00fa69bd-bede-4918-a017b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1529318977</date></change><change><date>1529319116</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1529481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>9726910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1528120494</date></change><change><date>1528123478</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2068839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284665</date></change><change><date>1406284666</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7603071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1400325753</date></change><change><date>1400325754</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284761</date></change><change><date>1406284762</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
A logically equivalent awk script which produces the same output as the sed code above:
/^dn:/
if (hold ~ "o=acme#6#")
print hold
hold = $0;
getline
hold = hold substr($0, 2)
next
/^rdcPosition:/
line = $0
while (line !~ "</position>")
getline
line = line substr($0, 2)
if (line ~ "o=acme#6#")
hold = hold ORS line
END
if (hold ~ "o=acme#6#")
print hold
The substr($0, 2) calls will strip the leading space off of the broken lines in the input.
Both scripts assumes that the dn line is broken into exactly two lines.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using the following sed script (it assumes we're running it with sed -n):
/^dn:/ # this is a "dn" line
N; # append the next line
s/n //; # remove the newline and the space
x; # exchange pattern space with hold space
/o=acme#6#/p; # print if pattern space contains our string
d; # delete from pattern space, start next cycle
/^rdcPosition:/ # this is a "rdcPosition" line
:again; # define label for loop
N; # append the next line
s/n //; # remove the newline and the space
#</position>#!b again; # if the end tag "</position>" was not read, loop
/o=acme#6#/H; # append to hold space if matching what we're looking for
$ # at the very end of input
x; # exchange pattern and hold space
/o=acme#6#/p; # print if pattern space contains our string
What the script does is essentially to build up a string in the sed "hold space" (general purpose buffer that survives between cycles). The string will start with the dn line and then it will be appended with the rdcPosition lines that contain the particular string that we are interested in.
Whenever a new dn line is found, or when we are at the end of the input, the hold space is conditionally printed if it contains our string (it may not contain it if none of the rdcPosition lines for the current dn line matched).
Testing it:
$ sed -n -f script.sed file
dn: cn=00fa69bd-bede-4918-a017b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1529318977</date></change><change><date>1529319116</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1529481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>9726910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1528120494</date></change><change><date>1528123478</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2068839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284665</date></change><change><date>1406284666</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7603071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1400325753</date></change><change><date>1400325754</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284761</date></change><change><date>1406284762</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
A logically equivalent awk script which produces the same output as the sed code above:
/^dn:/
if (hold ~ "o=acme#6#")
print hold
hold = $0;
getline
hold = hold substr($0, 2)
next
/^rdcPosition:/
line = $0
while (line !~ "</position>")
getline
line = line substr($0, 2)
if (line ~ "o=acme#6#")
hold = hold ORS line
END
if (hold ~ "o=acme#6#")
print hold
The substr($0, 2) calls will strip the leading space off of the broken lines in the input.
Both scripts assumes that the dn line is broken into exactly two lines.
Using the following sed script (it assumes we're running it with sed -n):
/^dn:/ # this is a "dn" line
N; # append the next line
s/n //; # remove the newline and the space
x; # exchange pattern space with hold space
/o=acme#6#/p; # print if pattern space contains our string
d; # delete from pattern space, start next cycle
/^rdcPosition:/ # this is a "rdcPosition" line
:again; # define label for loop
N; # append the next line
s/n //; # remove the newline and the space
#</position>#!b again; # if the end tag "</position>" was not read, loop
/o=acme#6#/H; # append to hold space if matching what we're looking for
$ # at the very end of input
x; # exchange pattern and hold space
/o=acme#6#/p; # print if pattern space contains our string
What the script does is essentially to build up a string in the sed "hold space" (general purpose buffer that survives between cycles). The string will start with the dn line and then it will be appended with the rdcPosition lines that contain the particular string that we are interested in.
Whenever a new dn line is found, or when we are at the end of the input, the hold space is conditionally printed if it contains our string (it may not contain it if none of the rdcPosition lines for the current dn line matched).
Testing it:
$ sed -n -f script.sed file
dn: cn=00fa69bd-bede-4918-a017b59b0901bb3d,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=1950,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8946702990</cn><reqdate>1529318977</reqdate><startdate>1529318977</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1529318977</date></change><change><date>1529319116</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1529481285</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=010903cd-e92d-4307-bffc-4921379153c0,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=311982,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>9726910833</cn><reqdate>1528120494</reqdate><startdate>1528120494</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1528120494</date></change><change><date>1528123478</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change></lifecycle></position>
dn: cn=01126aa4-af80-401b-8713-29e360868999,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
rdcPosition: cn=914570,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2068839799</cn><reqdate>1406284665</reqdate><startdate>1406284665</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284665</date></change><change><date>1406284666</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1435847283</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=999546,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7603071057</cn><reqdate>1400325753</reqdate><startdate>1400325753</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1400325753</date></change><change><date>1400325754</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224475</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
rdcPosition: cn=3513,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2802042129</cn><reqdate>1406284761</reqdate><startdate>1406284761</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1406284761</date></change><change><date>1406284762</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1449224599</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
A logically equivalent awk script which produces the same output as the sed code above:
/^dn:/
if (hold ~ "o=acme#6#")
print hold
hold = $0;
getline
hold = hold substr($0, 2)
next
/^rdcPosition:/
line = $0
while (line !~ "</position>")
getline
line = line substr($0, 2)
if (line ~ "o=acme#6#")
hold = hold ORS line
END
if (hold ~ "o=acme#6#")
print hold
The substr($0, 2) calls will strip the leading space off of the broken lines in the input.
Both scripts assumes that the dn line is broken into exactly two lines.
answered 8 hours ago
Kusalananda
100k13199311
100k13199311
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460626%2fsearch-for-a-pattern-and-always-print-the-first-line-which-contains-the-cn%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Does the data have those wrapping newlines in it?
â Kusalananda
yesterday
yes it does. the file is split on newline starting with dn: cn=
â user303833
18 hours ago