gpt4 book ai didi

bash - 独立于操作系统转换 Redis 批量插入协议(protocol)格式

转载 作者:IT王子 更新时间:2023-10-29 06:13:03 27 4
gpt4 key购买 nike

我正在尝试使用命令 cat data.txt | 执行 Redis 批量插入redis-cli --pipehttps://redis.io/topics/mass-insert 中所述.

必须转换 macOS 上的数据格式,以便可以使用 cat ${FILE} | 执行批量插入perl -i -p -e 's|[\r\n]+|\r\n|g' | redis-cli --pipe.

但是,上述命令不适用于 Linux 环境(或使用基于 alpine 的镜像构建容器的 docker 环境)。相反,必须执行以下命令 cat ${FILE} | sed 's/\r*$/\r/' | redis-cli --pipe.

有没有在两种环境下都适用的命令?

编辑:附上以下内容:

  1. Alpine Linux 上的 Redis 批量插入脚本:https://gist.github.com/francjohny/f2b13b4cfc147e07e52824ec88ba3781

  2. Mac 操作系统上的 Redis 批量插入脚本:https://gist.github.com/francjohny/b57756a1e0124dd562959ca5ece2a32b

  3. Redis 协议(protocol)格式数据文件:https://gist.github.com/francjohny/0c21f32d9902809b215f4e92f5e6a9f1

  4. ➜ head ouput.rpf| xxd - Mac 操作系统:https://gist.github.com/francjohny/e1a646ab44e7edd7374d28e9ca400711

  5. ➜ head ouput.rpf| xxd - Alpine Linux:https://gist.github.com/francjohny/252904928ded4c045448d12b205228df

最佳答案

更新的答案

从您添加的数据来看,您似乎只是用换行符分隔行,而 Redis 需要回车后跟换行符。所以基本上,你需要 unix2dos 程序的等价物,它不包含在 macOS 中。但是,macOS 确实包含 Perl,因此您应该能够使用:

perl -pe 's/\n/\r\n/' data.rpf | redis-cli --pipe

它在我的 Mac 上运行良好。

原始答案

您似乎在各种环境中都有混合的行尾。我想这个 Perl 会像 Redis 要求的那样用单个回车和换行替换任意数量的回车和换行:

perl -pe 's|[\r\n]*|\r\n|' data.txt | redis-cli ...

如果没有,请在评论中回答我的问题。

关于bash - 独立于操作系统转换 Redis 批量插入协议(protocol)格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50080828/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com