If I have a file or output:
givenName: filip
gidNumber: 500
homeDirectory: /home/users/ffilip
sn: filip
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
userPassword:: MTIz
givenName: stiv
gidNumber: 500
homeDirectory: /home/users/stiv
sn: stiv
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
userPassword:: BhJk
I want to get :
givenName: filip, userPassword:: MTIz
givenName: stiv, userPassword:: BhJk
CodePudding user response:
This will work:
assuming you have the output
in a file:
cat output| egrep "givenName:|userPassword::"|paste -d ", " - -
or
ldap command|egrep "givenName:|userPassword::"|paste -d ", " - -
or to have the separation comma
with an additional space
you can use:
ldap command|egrep "givenName:|userPassword::"|paste -d ", " - -|sed 's/,/, /g'
Of course you can also accomplish this with awk
:
a bit modified to use a delimiter with only 1 space:
ldap command|awk '$1=="givenName:" {printf "%s", $0} $1=="userPassword::" {print ",", $0}'
or Perl's regex functionality.. as the other answers suggest.
CodePudding user response:
Another option is awk
. Simply matching the first field and outputting the record without a newline when it is "givenName:"
and then concatenating a ", "
and outputting the "userPassword::"
record with a newline, e.g.
awk '$1=="givenName:" {printf "%s", $0} $1=="userPassword::" {print ", ", $0}' file
Example Use/Output
With your output in the file named output
you would have:
$ awk '$1=="givenName:" {printf "%s", $0} $1=="userPassword::" {print ", ", $0}' output
givenName: filip, userPassword:: MTIz
givenName: stiv, userPassword:: BhJk
(if you want a space between the lines you can output an additional newline after the userPassword::
or before givenName:
. You can set a flag to prevent an additional newline before the first record. Up to you)
CodePudding user response:
A sed
one-liner:
sed -n '/^givenName:/h;/^userPassword:/{H;g;s/\n/, /p;}' file
CodePudding user response:
With GNU ed
(1).
printf '%s\n' 'v/^givenName:\|^userPassword::/d' 'g/.\{1,\}/; 1j' ,p Q | ed -s file.txt