Issue
I have a file that list the attributes found using ldap search like this
dsee_simpleauthbinds{cn="1389"} 141962
dsee_simpleauthbinds{cn="1636"} 1029264
dsee_wholesubtreesearchops{cn="1389"} 1638369
dsee_wholesubtreesearchops{cn="snmp"} 35862
the attributes are grouped together in 'sessions' eg the wholesubtreesearchops
attribute is grouped with all the cn where that attribute is present.
I need to modify the file so that for each attribute, the attribute is added before the start of the session so the file above becomes this:
[dsee_simpleauthbinds]
dsee_simpleauthbinds{cn="1389"} 141962
dsee_simpleauthbinds{cn="1636"} 1029264
[dsee_wholesubtreesearchops]
dsee_wholesubtreesearchops{cn="1389"} 1638369
dsee_wholesubtreesearchops{cn="snmp"} 358629
of course there are far more attributes the idea would be before each new attribute found in the file add the header in the line just before it.
Solution
Assumptions:
- data is already sorted by attribute
- the only requirement is to add a
[<attribute>]
line, ie, thenode="...",
strings in the expected output are typos; otherwise OP needs to update the question with the details on where thenode="...",
strings are coming from
Sample input data:
$ cat attrib.dat
dsee_simpleauthbinds{cn="1389"} 141962
dsee_simpleauthbinds{cn="1636"} 1029264
dsee_wholesubtreesearchops{cn="1389"} 1638369
dsee_wholesubtreesearchops{cn="snmp"} 35862
One awk
idea:
awk -F'{' '$1 != header {print "[" $1 "]"; header=$1}1' attrib.dat
This generates:
[dsee_simpleauthbinds]
dsee_simpleauthbinds{cn="1389"} 141962
dsee_simpleauthbinds{cn="1636"} 1029264
[dsee_wholesubtreesearchops]
dsee_wholesubtreesearchops{cn="1389"} 1638369
dsee_wholesubtreesearchops{cn="snmp"} 35862
Answered By - markp-fuso Answer Checked By - Mary Flores (WPSolving Volunteer)