How to export public key from Certificate Signing Request?

To output only the public key to a local file named publickey.pem:
openssl req -in csr.txt -noout -pubkey -out publickey.pem

You can view the (PEM-encoded) key on the terminal without putting it in a file by dropping the last argument:
openssl req -in csr.txt -noout -pubkey

Note: the -noout option is required, as by default the entire CSR will be placed in the output file, while your question asks for the public key only.

Bonus points: To look inside the BASE64-encoded PEM output and see the actual public key in hex format, pipe it to the pkey function of openssl:
openssl req -in csr.txt -noout -pubkey | openssl pkey -pubin -noout -text


openssl req -in file.csr -pubkey -out pubkey.pem