Search code examples
shellgrepxargs

How to pipe the each output of grep to another command?


I have a command the returns a list of tables in the database:

drush sqlq "show tables"|grep deleted

This returns a list of tables that need to be removed:

field_deleted_data_811d267471
field_deleted_data_e52abfde52
field_deleted_data_eefd3bb8fd

I am trying to take each result line and pipe in into a command

drush sqlq "drop table TABLENAME"

I am trying to build somehting like drush sqlq "show tables"|grep deleted |xargs -n1 drush sqlq "drop table ${TABLE_NAME}" where TABLE_NAME each line of the output.

I saw the question but it still remains unclear how you can access each result line in the piped command.


Solution

  • xargs has -I<STRING> that will be replaced before running the command, in your case would be e.g.:

    drush sqlq "show tables"|grep deleted |xargs -I@ -n1 drush sqlq "drop table @"