2013-03-21 88 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