将配置文件中的某个部分信息移动到另一个部分之上。

huangapple go评论71阅读模式
英文:

Move certain section information above another section in a configuration file

问题

抱歉,您的代码部分不需要翻译,只需要对您的问题进行解答。根据您提供的信息,问题似乎是关于脚本在将[mysqld]下的内容移到顶部时遇到问题的。以下是关于如何解决问题的建议:

问题似乎出现在脚本尝试移动[mysqld]下的内容时,由于文件中存在空行("----GAPS----")导致只有一部分内容被复制。要解决这个问题,您可以修改脚本以跳过空行并继续查找下一个非空行。这里是一个可能的修改:

#!/bin/bash
FILENAME='my.cnf'
LINE_NUMBER1=$1
LINE_NUMBER2=$2

# 使用awk来处理文件
awk -v start="$LINE_NUMBER1" -v end="$LINE_NUMBER2" '
{
    # 如果当前行号大于等于start且小于等于end,跳过当前行
    if (NR >= start && NR <= end) {
        next
    }

    # 打印当前行
    print
}
' "$FILENAME" > temp_file

# 复制移动的内容到顶部
sed -n "/^\[mysqld\]/r temp_file" "$FILENAME" > new_file

# 用新文件替换原文件
mv new_file "$FILENAME"

# 删除临时文件
rm temp_file

此修改将使用awk来处理文件,跳过在指定行范围内的内容,然后使用sed将内容复制到[mysqld]部分之上。希望这可以解决您的问题。

英文:

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=&#39;my.cnf&#39;
LINE_NUMBER1=$1
LINE_NUMBER2=$2
declare -a LINE_CONTENT=$(sed -n &quot;${LINE_NUMBER1},${LINE_NUMBER2}&quot;p &quot;$FILENAME&quot;)
sed -i &quot;${LINE_NUMBER1},${LINE_NUMBER2}&quot;&#39;d&#39; &quot;$FILENAME&quot;
for value in &quot;${LINE_CONTENT[@]}&quot;; do
sed -i &quot;/^#/n;/\[mysqld]/i ${value}&quot; &quot;$FILENAME&quot;
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=&quot;NO_ENGINE_SUBSTITUTION&quot;
-----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

答案1

得分: 1

ed来拯救:

ed -s my.cnf &lt;&lt;&#39;EOF&#39;
/^\[mysqld\]$/;/^$/m$
?\[mysqld\]?i

.
w
EOF

首先找到以[mysqld]开头并以下一个空行结束的块,将其移到文件的末尾,并确保在它之前插入一个空行,以确保它不会与前一个最终块混合在一起,然后将更改后的文件写回。

英文:

ed to the rescue:

ed -s my.cnf &lt;&lt;&#39;EOF&#39;
/^\[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.

答案2

得分: 1

这是一个(有点冗长的)SED脚本,可能会完成任务。

#! /bin/sh

MOVE_SECTION='mysqld'
AFTER_SECTION='sst'

SED_SCRIPT="$(cat <<-_EOF_
#n
/^\[/!{p;b}

/^\[${MOVE_SECTION}\]/{
  :hold
  H
  n
  /^\[/!b hold
}

/^\[${AFTER_SECTION}/!{p;b}
:loop
p
$b append
n
/^\[/!b loop
H

:append
x
p

_EOF_
)"

sed -e "$SED_SCRIPT" INPUTFILE

给定以下输入:

[client]

a=99

b=101


[mysqld]
blah=this
b=1
c=2


[myisamchk]

a=1,2
d=22
e=name

[sst]
f=yes
this=that

[other]
blah=1
blih=2
bloh=3

它应该生成:

[client]

a=99

b=101


[myisamchk]

a=1,2
d=22
e=name

[sst]
f=yes
this=that


[mysqld]
blah=this
b=1
c=2


[other]
blah=1
blih=2
bloh=3

如果需要调整,请告诉我。

编辑

删除了 "foundSection" 循环(不需要)。

英文:

Here's a (somewhat lengthy) SED script that might do the job.

#! /bin/sh

MOVE_SECTION=&#39;mysqld&#39;
AFTER_SECTION=&#39;sst&#39;


SED_SCRIPT=&quot;$(cat &lt;&lt;-_EOF_
#n
/^\[/!{p;b}

/^\[${MOVE_SECTION}\]/{
  :hold
  H
  n
  /^\[/!b hold
  }

/^\[${AFTER_SECTION}/!{p;b}
:loop
p
$b append
n
/^\[/!b loop
H

:append
x
p

_EOF_
)&quot;

sed -e &quot;$SED_SCRIPT&quot; INPUTFILE

Given the following input:

[client]

a=99

b=101


[mysqld]
blah=this

b=1

c=2


[myisamchk]

a=1,2

d=22

e=name

[sst]

f=yes
this=that

[other]
blah=1

blih=2

bloh=3

... it should produce:

[client]

a=99

b=101


[myisamchk]

a=1,2

d=22

e=name

[sst]

f=yes
this=that


[mysqld]
blah=this

b=1

c=2


[other]
blah=1

blih=2

bloh=3

Let me know if it needs adjustments.

HTH

Edit:

Removed "foundSection" loop (unneeded).

huangapple
  • 本文由 发表于 2023年7月23日 20:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定