2013-03-21 87 views
0

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

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

+0

这似乎是重复http://stackoverflow.com/questions/148451/how-to-use-sed-to的-re-only-the-first-in-a-file – oikku 2013-03-21 07:25:04

回答

1

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

sed 's//1 /' 
+0

我想替换前三个。怎么做?? – 2017-08-24 06:04:16

+0

输入:'''1972? 00:25:05 python 21408? 00:05:31 python help.py 27518? 00:00:00 python insert \ node.py 32690? 02:51:20 python monitor \ the \ database.py''' 输出: 27518 |?| 00:00:00 | python insert \ node.py 32690 |?| 02:51:20 | python monitor \ \ database.py – 2017-08-24 09:01:12

+0

@RaviShankerReddy:不幸的是,没有格式化就不可能理解数据。我建议你把它作为一个单独的问题发布,你会得到更快的帮助。 – anubhava 2017-08-24 10:09:15