I needed a quick way to get the machine names of enabled/installed modules on an existing site. The pml command is pretty good at providing that information:

drush pml | grep 'Enabled'
 Administration       Admin Toolbar (admin_toolbar)                     Module  Enabled        8.x-1.19
 Core                 Automated Cron (automated_cron)                   Module  Enabled        8.3.2
 Core                 Block (block)                                     Module  Enabled        8.3.2
 Core                 Breakpoint (breakpoint)                           Module  Enabled        8.3.2
 Core                 CKEditor (ckeditor)                               Module  Enabled        8.3.2

Of course, we’d need to ensure some uninstalled project with the word “Enabled” does not slip into this list.

The output above has the machine name in parenthesis. I need to put these names in an install profile’s .info.yml file so I only need the machine names. Awk to the rescue:

drush pml | grep 'Enabled' | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }'

This will return something like:

admin_toolbar
automated_cron
block
breakpoint
ckeditor

One can make sense of the world with sed, awk, and grep.

Resources