1

for example I have a string like "test test test " in a file. I want to replace the first space with ' 1 ' to make it "test 1 test test test " .

I tried sed -i "s/^\ /\ 1\ /g" text.txt but it will change all spaces to ' 1 ', so I would like to know how to make it only happen on the first found?

tomriddle_1234
  • 3,145
  • 6
  • 41
  • 71
  • This seems to be duplicate of http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file – oikku Mar 21 '13 at 07:25

1 Answers1

2

Don't use 'g' option (global replacement) like this:

sed 's/ / 1 /'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I want to replace first three. How to do that?? – Ravi Shanker Reddy Aug 24 '17 at 06:04
  • Input : ``` 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``` Output: 27518|?|00:00:00|python insert\ node.py 32690|?|02:51:20|python monitor\ the\ database.py – Ravi Shanker Reddy Aug 24 '17 at 09:01
  • @RaviShankerReddy: Unfortunately without formatting it is impossible to understand data. I suggest you post it as a separate question and you will get faster help. – anubhava Aug 24 '17 at 10:09