Moin,
nice exercise, I came up with the following:
printf "firstname,lastname,mail,school\n" && udm users/user list --filter ucsschoolRole="student:school:school2" | grep -E "(^$|((first|last)name|school|mailPrimaryAddress): )" | awk '{if(NF){gsub(/ |,$|((first|last)name|school|mailPrimaryAddress): /,""); printf c $0; c=","}else{printf "\n"; c=""}};END{printf "\n"}'
firstname,lastname,mail,school
Domenic,Tremper,domenicm.tremperm@school2.domain.de,school2
Domenica,Trentmann,domenicam.trentmannm@school2.domain.de,school2
To explain it a bit:
# print the header
printf "firstname,lastname,mail,school\n" \
# list all users with the ucsschoolRole student at school2
&& udm users/user list --filter ucsschoolRole="student:school:school2" \
# Only list empty lines and lines containing the attributes needed
| grep -E "(^$|((first|last)name|school|mailPrimaryAddress): )" \
# Remove the attribute prefixes and build a CSV from the line input, adapted from https://unix.stackexchange.com/a/530426
| awk '{if(NF){gsub(/ |,$|((first|last)name|school|mailPrimaryAddress): /,""); printf c $0; c=","}else{printf "\n"; c=""}};END{printf "\n"}'
While this works in my example there are several things to consider:
- This only works if every user contains the same amount of the same attributes, otherwise the number of values per line differs
- spaces are removed completely
- Nothing is escaped or sanitized
I would not consider this to be usable in production, but maybe you can start with this and develop it further. If you do that please post your solution afterwards.
Best regards
Jan-Luca