[:en]Linux – The sed command[:]

By admin, April 10, 2018

[:en]This very flexible tool present in Linux distributions is used for a plethora of use cases related to text processing, most taking care of all possible exceptions.

I had the need to read the manual again in order to be able to apply the simplest way (imho) for get rid of sections of text. My use case would be erase not needed table from a SQL dump file. In MySQL case, I had a huge audit table called tracker and another one called activities

Thus, I have tested the commands individually:

$ sed ‘/^START$/,/^END$/d’ inputfile

In my case:

$sed ‘/DROP TABLE.*`tracker`/,/UNLOCK TABLES/d ‘ backup.sql > tables.sql

$sed ‘/DROP TABLE.*`activities`/,/UNLOCK TABLES/d ‘ tables.sql > tables.sql

For the sake of performance and simplicity, the final command was

$sed ‘/DROP TABLE.*`activities.*`/,/UNLOCK TABLES/d ; /DROP TABLE.*`tracker.*`/,/UNLOCK TABLES/d’ tables.sql > tables_cleaned.sql

Finally, just for the records, if you want to save just a subset or a table, in this case, it could be done as

$sed -n -e ‘/DROP TABLE.*`accounts.*`/,/UNLOCK TABLES/p’ backup.sql > accounts.sql

 

Reference

I have found the mentioned manual at

https://www.digitalocean.com/community/tutorials/intermediate-sed-manipulating-streams-of-text-in-a-linux-environment[:]