I pulled some sample queries from Google and made a couple sql files with the following data in them for example purposes:
file1.sql
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2013
FROM CustomersOriginal
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
file2.sql
-- Dumping data for table `user_details`
INSERT INTO `user_details` (`user_id`, `username`, `first_name`, `last_name`, `gender`, `password`, `status`) VALUES
(1, 'rogers63', 'david', 'john', 'Female', 'e6a33eee180b07e563d74fee8c2c66b8', 1);
The first thing you're looking for is the OR operator for grep:
$ grep 'INTO\|FROM\|JOIN' *.sql
file1.sql:FROM Orders
file1.sql:INNER JOIN Customers
file1.sql:INTO CustomersOrderBackup2013
file1.sql:FROM CustomersOriginal
file1.sql:LEFT JOIN Orders
file2.sql:INSERT INTO `user_details` (`user_id`, `username`, `first_name`, `last_name`, `gender`, `password`, `status`) VALUES
That will get the entire lines from the original files. Next let's use sed to pull out the table names:
$ grep 'INTO\|FROM\|JOIN' *.sql | sed -r 's/.*?(FROM|INTO|JOIN)\s`?([^` ]*).*/\2/g'
Orders
Customers
CustomersOrderBackup2013
CustomersOriginal
Orders
user_details
Finally, let's eliminate duplicate tables:
$ grep 'INTO\|FROM\|JOIN' *.sql | sed -r 's/.*?(FROM|INTO|JOIN)\s`?([^` ]*).*/\2/g' | sort -u
Customers
CustomersOrderBackup2013
CustomersOriginal
Orders
user_details
grep
has no input other than STDIN, which it ignores, and produces no output for the (ignored) input of the secondgrep
. Read http://askubuntu.com/help/how-to-ask – waltinator Jul 11 '16 at 16:58