Script - cut part of a text file between two strings

Costas

Administrator
Staff member
everyday getting a 150mb mySQL dump file (exported by phpmyadmin), contains a logs table with 28k rows kai 20fields, needed to cut out the table from this file.

to accomplish this we have to

find the line number of first occurrence (aka the line that writes the table name) :
-- Table structure for table `logs`

find the line number of first occurrence of next table name (aka the line that writes the table name) :
-- Table structure for table `next_table`

save this to test.sh with your favorite text editor
JavaScript:
#!/bin/bash
#https://stackoverflow.com/a/10885460
startno=$(grep -m 1 -nr "platformlogs" source.sql | cut -f1 -d:)
endno=$(grep -m 1 -nr "\`platformroles\`" source.sql | cut -f1 -d:)

#https://stackoverflow.com/a/5683408
sed -n "$startno,$endno w source_cleaned.sql" source.sql


sample2:
JavaScript:
#!/bin/bash
#https://www.cyberciti.biz/faq/sed-howto-remove-lines-paragraphs/
sed '/platformlogs/,/\`platformroles\`/d' source.sql > source_cleaned.sql



JavaScript:
#to make it runnable, execute :
chmod u+x test.sh

#execute it with :
./test.sh

the dump file
before : 147mb
after : 7.6mb
 
Top