Issue
I have a requirement to move all the sections and its values present below [mysqld] to above it in a mysql configuration file and after moving those contents they need to be deleted,I have tried the below script but it is just adding variable name instead of the contents and deletes the contents,Can anyone help resolve it or it would be appreciated if i get code to automatically move the sections and its content present below [mysqld] to the top of [mysqld] section
output:
[client]
a=99
${value}
[mysqld]
b=1
c=2
script:
#!/bin/bash
FILENAME='my.cnf'
LINE_NUMBER1=$1
LINE_NUMBER2=$2
declare -a LINE_CONTENT=$(sed -n "${LINE_NUMBER1},${LINE_NUMBER2}"p "$FILENAME")
sed -i "${LINE_NUMBER1},${LINE_NUMBER2}"'d' "$FILENAME"
for value in "${LINE_CONTENT[@]}"; do
sed -i "/^#/n;/\[mysqld]/i \${value}" "$FILENAME"
done
Current file:my.cnf
[client]
a=99
[mysqld]
b=1
c=2
[myisamchk]
a=1,2
d=22
e=name
[sst]
f=yes
Expected after script execution
[client]
a=99
[myisamchk]
a=1,2
d=22
e=name
[sst]
f=yes
[mysqld]
b=1
c=2
Actual my.cnf:
[mysqld]
disable-log-bin = 1
----GAPS----
skip-name-resolve = 1
performance-schema = 0
local-infile = 0
mysqlx = 0
open_files_limit = 200000
max_allowed_packet = 256M
sql_mode="NO_ENGINE_SUBSTITUTION"
-----GAPS------
innodb_dedicated_server = 1
innodb_buffer_pool_instances = 48
[myisamck]
a=3
b=4
result is:
only the below is being copied below [myisamchk] if there are gaps,if there are no gaps then it works fine:
[mysqld]
disable-log-bin = 1
Solution
ed
to the rescue:
ed -s my.cnf <<'EOF'
/^\[mysqld\]$/;/^$/m$
?\[mysqld\]?i
.
w
EOF
First find the block starting with the line [mysqld]
and ending with the next blank line, move it to the end of the file, and make sure to insert a blank line before it so it doesn't blend into the previous final block, then write the changed file back out.
Answered By - Shawn Answer Checked By - Marilyn (WPSolving Volunteer)