To expand on the previous reply:
Download a CSV file of students
- Go to Site administration ▸ Users ▸ Bulk user actions.
- Under New filter click Show more....
- Find Course role:
- Change any role to Student.
- At the end of the list click Add filter.
- Change any role to Student.
- Under Users in list in Users select All filtered then click Add to selection.
- At the bottom of the page in With selected users... select Download and click Go.
- In Export users as leave this as Comma separated values (.csv) and click Download and save the file.
Add the new username in the CSV file
- Open the downloaded file in a spreadsheet program, e.g. Excel.
- Change the first row of the username column to oldusername.
- Insert a new column, e.g. column B, and enter username in the first row. This column will contain the new username.
- In row 2 of this column enter the new username using a formula. If the email address is in column D in LibreOffice Calc this would be =LEFT(D2, SEARCH("@", D2) - 1), it may be the same in Excel.
- Copy and paste this formula into the remaining rows.
- Save the file. This must be saved as in CSV format (not .xlsx, .ods, etc.).
Upload the updated CSV file
- Go to Site administration ▸ Users ▸ Upload users.
- Select the updated CSV file and click Upload users.
- On the Upload users preview page under Settings:
- change Upload type to Update existing users only,
- change Allow renames to Yes.
- Click Upload users.
- The Upload users results page should show the username change as expected.
I would recommend making this change with a small number of users first to test it works as expected, ideally try it on a test or staging site if available.