Skip to content

Instantly share code, notes, and snippets.

@quintindk
Created November 14, 2022 10:40
Show Gist options
  • Save quintindk/8d57b32f56ba5fe4a43c6a0bbc72d118 to your computer and use it in GitHub Desktop.
Save quintindk/8d57b32f56ba5fe4a43c6a0bbc72d118 to your computer and use it in GitHub Desktop.
So I improved the previous script using AWK which is relatively simple when you understand that it handles each line individually in a stream i.e. if you need previous lines keep them in a buffer until you're ready to dump them.
#! /bin/bash
database_name=$1
input_file=$2
output_file=$3
temp_file=$(mktemp)
sed '/SET @@SESSION.GTID_NEXT=/d' "$input_file" > "$temp_file"
awk '{
if ($0 ~ "Table_map: `'"$database_name"'`") {
included=1
buffer=buffer"\n"$0
}
else if ($0 ~ "COMMIT\/") {
if (included==1) {
print buffer"\n"$0
}
included=0
started=0
buffer=""
}
else if ($0 ~ "BEGIN") {
started=1
buffer=buffer"\n"$0
}
else if (started==1) {
buffer=buffer"\n"$0
}
}' "$temp_file" > "$output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment