【问题标题】:Special characters problem in variable/file. Same string, different format from different sources变量/文件中的特殊字符问题。相同的字符串,不同来源的不同格式
【发布时间】:2021-10-08 15:44:56
【问题描述】:

我有一个编码问题,我无法解决。 我对 linux 和 bash 也很陌生,所以请耐心等待。

上下文/示例:

cat file1.txt
Foo ヅ

#file -i file1.txt: text/plain; charset=utf-8
#Source: website curl
cat file2.txt
Foo ヅ

#file -i file1.txt: text/plain; charset=utf-8
#Source: mysql database query (result is the import of file1.txt)

如果我将 file1.txt 插入我的数据库,它会显示“Foo ãƒ...”。我尝试了各种转换、排序规则等。它在 mysql 中永远不会显示正确的字符 - 但我很好。

问题: 我需要检查这些字符串是否与 if 语句相同:

var1=$(cat file1.txt )
var2=$(cat file2.txt )

if [ "$var1" != "$var2" ]; then
    #stuff is done
fi

我什至不记得我曾尝试使用 iconv 将 var1 或 var2 转换为彼此匹配的所有内容,因此我的 if 语句可以按预期工作。 我唯一的解决方法是将 file1.txt 导入我的数据库中的另一个表并再次提取它,但我使用的数据库连接数量有限。

非常感谢任何有关如何更轻松地解决此问题的提示!

【问题讨论】:

  • Source: mysql database query Please show mysql database table definition.stackoverflow.com/questions/202205/… I need to check if these strings 用相同的设置创建一个临时的mysql表,将字符串放入其中,取回,然后比较。 but i'm working with a limited amount of DB connections用seaprate数据库启动本地mysqld。

标签: linux string bash encoding


【解决方案1】:

感谢卡米尔库克!问题是数据库本身的排序规则(我什至不知道这是一件事)。

将数据库排序规则和表排序规则设置为 utf8mb4_unicode_ci 修复了导入时的编码,因此整个问题都解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多