一行中的两个变量

发布于 2024-09-14 14:57:37 字数 337 浏览 6 评论 0原文

损坏的表条目记录在错误文件中,如下所示......

/usr/local/mysql/bin/mysqld:不正确 表的密钥文件 './accounts/headers.MYI';尝试 修复它

我想编写一个查询来修复此表。

修复表accounts.headers;

我需要做的是搜索“./”的第一个实例并选择第一个单词,即“accounts”,然后选择下一个单词“headers”。 现在使用这两个变量编写如上所示的语句。 我应该为此目的编写一个 Shell 脚本吗?

The corrupt table entry is logged in the error file something like this...

/usr/local/mysql/bin/mysqld: Incorrect
key file for table
'./accounts/headers.MYI'; try to
repair it

I want to write a query that will repair this table.

REPAIR TABLE accounts.headers;

What I need to do is search for the first instance of "./" and select the first word i.e. "accounts" and choose the next word "headers".
Now use these two variables to write a statement like shown above.
Should I write a Shell script for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春花秋月 2024-09-21 14:57:37

您可以使用 grepsed 来执行此操作:

pax> cat qq.in
/blah: Incorrect key file for table './accounts/headers.MYI'; try blah
/blah: Incorrect key file for table './pax/diablo.myi'; try blah

pax> grep 'Incorrect key file for ' qq.in | sed
     -e 's/.*\.\//REPAIR TABLE /g'
     -e 's/\//./'
     -e 's/\.[Mm][Yy][Ii].*/;/g'
REPAIR TABLE accounts.headers;
REPAIR TABLE pax.diablo;

请注意,为了便于阅读,我已将所有这些 -e 部分放在不同的行上。它们应该全部在一根线上进行测试。

如果您的 sed 版本支持不区分大小写的搜索(如 GNU 版本),您可以将最后一个替换替换为 's/\.myi.*/;/Ig'.


捕获具有不同格式的行有点棘手,并且需要知道所有格式以避免误报。以下命令将捕获您的评论中提供的替代格式:

pax> cat qq.in
/blah: Incorrect key file for table './accounts/headers.MYI'; try blah
/blah: Incorrect key file for table './linus/torvalds.myi'; try blah
/usr/local/mysql/bin/mysqld: Table './beta/search_data' is marked as crashed

allachan@IBM-L3F3936 ~
$ egrep 'Incorrect key|as crashed' qq.in | sed
    -e "s/.*\.\//REPAIR TABLE /g"
    -e "s/[\\.'].*/;/g"
    -e "s/\//./"
REPAIR TABLE accounts.headers;
REPAIR TABLE linus.torvalds;
REPAIR TABLE beta.search_data;

You can use grep and sed to do this:

pax> cat qq.in
/blah: Incorrect key file for table './accounts/headers.MYI'; try blah
/blah: Incorrect key file for table './pax/diablo.myi'; try blah

pax> grep 'Incorrect key file for ' qq.in | sed
     -e 's/.*\.\//REPAIR TABLE /g'
     -e 's/\//./'
     -e 's/\.[Mm][Yy][Ii].*/;/g'
REPAIR TABLE accounts.headers;
REPAIR TABLE pax.diablo;

Note that I've put all those -e sections on different lines for readability. They should be all on one line to test.

If your version of sed supports case-insensitive searches (like the GNU one does), you can replace that last substitution with 's/\.myi.*/;/Ig'.


To catch lines with different foramts is a bit trickier and will require all the formats to be known to avoid false positives. The following command will catch the alternate format as supplied in your comment:

pax> cat qq.in
/blah: Incorrect key file for table './accounts/headers.MYI'; try blah
/blah: Incorrect key file for table './linus/torvalds.myi'; try blah
/usr/local/mysql/bin/mysqld: Table './beta/search_data' is marked as crashed

allachan@IBM-L3F3936 ~
$ egrep 'Incorrect key|as crashed' qq.in | sed
    -e "s/.*\.\//REPAIR TABLE /g"
    -e "s/[\\.'].*/;/g"
    -e "s/\//./"
REPAIR TABLE accounts.headers;
REPAIR TABLE linus.torvalds;
REPAIR TABLE beta.search_data;
夜血缘 2024-09-21 14:57:37
#!/bin/bash
while read -r line
do
 case "$line" in
   *repair*)
    line="${line##*for table}"
    line="${line%%;*}"
    line=${line#*.\/}
    IFS="/"
    set -- $line
    echo "REPAIR TABLE $1.${2%.MYI*}"
 esac
done <"file"

或者只是 sed

$ sed 's|.*\.\/|REPAIR TABLE |;s|\/|.|;s|\.MYI.*||' file
REPAIR TABLE accounts.headers
#!/bin/bash
while read -r line
do
 case "$line" in
   *repair*)
    line="${line##*for table}"
    line="${line%%;*}"
    line=${line#*.\/}
    IFS="/"
    set -- $line
    echo "REPAIR TABLE $1.${2%.MYI*}"
 esac
done <"file"

or just sed

$ sed 's|.*\.\/|REPAIR TABLE |;s|\/|.|;s|\.MYI.*||' file
REPAIR TABLE accounts.headers
相对绾红妆 2024-09-21 14:57:37

以下正在做我期望它做的事情。但我对此不太满意,想知道更好的解决方案。

awk -F"'./" '{print $2}' | replace "'" ';' | replace "/" "." | replace '.MYI' '' | sed 's/^/; REPAIR TABLE /'

它将输出这样的语句...

; REPAIR TABLE kumar.headers;; try to repair it
; REPAIR TABLE raju.headers;; try to repair it

; 之后的文本当mysql执行时会抛出错误,但我可以忽略它。

The following is doing what I expect it to do. But I am not too happy with it and will like to know better solution.

awk -F"'./" '{print $2}' | replace "'" ';' | replace "/" "." | replace '.MYI' '' | sed 's/^/; REPAIR TABLE /'

It will output the statements like this...

; REPAIR TABLE kumar.headers;; try to repair it
; REPAIR TABLE raju.headers;; try to repair it

The text after ; will throw an error when mysql executes it, but I can ignore it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文