Command line - HowTo export Students to CSV File

Hello,
i need a list of all students of a certain school from the UCS domain, which contains the following:
First name, last name, school, class and email address.

Unfortunately, the export via the web interface contains all groups and is very confusing or has to be prepared in a time-consuming way.

Is there a way I can export the data to a CSV file via the command line?

Thanks

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:

  1. This only works if every user contains the same amount of the same attributes, otherwise the number of values per line differs
  2. spaces are removed completely
  3. 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. :slight_smile:

Best regards
Jan-Luca

thank you, i will test it.

Mastodon