linux - 使用 Bash 脚本将文件中的第一个空格替换为其他字符串

标签 linux bash

例如,我在一个文件中有一个类似“test test test”的字符串。 我想用“1”替换第一个空格,使其成为“test 1 test test test”。

我试过 sed -i "s/^\/\1\/g"text.txt 但它会将所有空格更改为 ' 1 ',所以我想知道如何制作它只发生在第一次发现时?

最佳答案

不要像这样使用 'g' 选项(全局替换):

sed 's/ / 1 /'

关于linux - 使用 Bash 脚本将文件中的第一个空格替换为其他字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15541568/

相关文章:

linux - 注销时后台进程 stdout 和 stderr 会发生什么?

c - 我如何计算整个项目中的代码行总数?

python - 在 Linux 系统上安装 Python-Tesseract

linux - Linux 上的 Jetty 不会被服务脚本停止

php - 如何从 php/html 执行 shell 脚本?

bash - 意外的输入结束 : expected close marker for Object error when trying to send a curl post request in a for loop

linux - 基于 Unix 时间戳过滤 Linux 日志

linux - 为什么现代操作系统需要这么多内存?

linux - 确保只有一个 Bash 脚本实例正在运行的最佳方法是什么?

bash - 在 MongoDB 查询中使用 shell 变量