Posts Script - cut part of a text file between two strings
Post
Cancel

Script - cut part of a text file between two strings

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

1
2
3
4
5
6
7
#!/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:

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

1
2
3
4
5
#to make it runnable, execute :
chmod u+x test.sh

#execute it with :
./test.sh

the dump file before : 147mb after : 7.6mb

origin - https://www.pipiscrew.com/?p=16347 script-cut-part-of-a-text-file-between-two-strings

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags