One easy answer to your question is to use dpkg
instead of apt
. apt
will happily do all the logic and keep track of the versions precisely so you needn't muck around with such tedious details.
This particular command shows currently-installed versions, not available-for-upgrade (use apt list --upgradeable
for that)
Try dpkg -l | grep ii | awk '{print$2, $3}' | sed 's/ /, /'
- Use
dpkg -l
to get the complete list of packages and versions in your package database - Limit the output by showing only installed (ii) packages:
grep ii
- Use awk to print only the second and third columns with :
awk '{print$2, $3}'
- Insert your comma using sed:
sed 's/ /, /'