1

I have a word like "mode v" in a file. I want to replace it to "mode sv". However all my efforts failed as there is space after mode

  • looks like i found the answer. sed 's/^mode .*$/mode sv/' – Artist In Sep 23 '14 at 21:45
1

You can have sed do the search and replacement like so.

$ sed 's/mode v/mode sv/g'

Example

Say I have this sample file:

$ cat afile.txt 
mode v modev mode v
blah
blah blah modev mode
blah
modev mode v

I can use sed to search/replace this file and preserve a backup like so:

$ sed -i.bak 's/mode v/mode sv/g' afile.txt 

I now have 2 files:

$ ls -ltr | grep afile
-rw-rw-r--. 1 slm slm      64 Sep 23 20:13 afile.txt.bak
-rw-rw-r--. 1 slm slm      67 Sep 23 20:15 afile.txt

With the changes made to the origianl:

$ cat afile.txt
mode sv modev mode sv
blah
blah blah modev mode
blah
modev mode sv

NOTE: If you need it to be a guaranteed word boundary you can suffix the mode v in the search with a \b.

$ sed -i.bak 's/mode v\b/mode sv/g' afile.txt 
1

Your solution from comment works only if mode is the very first word in the line and replaces everything after "mode " with "sv". From your question I believe this is not your intention. There is no reason to complicate things, simple

sed 's/mode /mode s/g'

should work. Alternatively

sed s/"mode "/"mode s"/g

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.