November 28, 2022

vi replace string

Vi/vim can use the :s command to replace strings. In the past, only one format was used to replace the full text. Today, I found that there are many ways to write this command, and record a few here for future reference.
:s/vivian/sky/ Replace the first vivian in the current line with sky
:s/vivian/sky/g Replace all vivian in the current line with sky
:n,$s/vivian/sky/ Replace the first vivian in each line from the nth line to the last line with sky
:n,$s/vivian/sky/g Replace all vivian in each line from the nth line to the last line with sky
n is a number, if n is ., it means from the current line to the last line
:%s/vivian/sky/ (equivalent to :g/vivian/s//sky/) Replace the first vivian in each line with sky
:%s/vivian/sky/g (equivalent to :g/vivian/s//sky/g) Replace all vivian in each line with sky
You can use # as a separator, and the / in the middle will not be used as a separator
:s#vivian/#sky/# Replace the first vivian/ in the current line with sky/
:%s+/oradata/apras/+/user01/apras1+ (use + to replace / ): /oradata/apras/ is replaced by /user01/apras1/


1.:s/vivian/sky/ Replace the first vivian in the current line with sky
:s/vivian/sky/g Replace all vivian in the current line with sky

  1. :n,$s/vivian/sky/ Replace the first vivian in each line from the nth line to the last line with sky
    :n,$s/vivian/sky/g Replace all vivian in each line from the nth line to the last line with sky
    (n is a number, if n is ., it means from the current line to the last line)
  2. :%s/vivian/sky/ (equivalent to :g/vivian/s//sky/) Replace the first vivian in each line with sky
    :%s/vivian/sky/g (equivalent to :g/vivian/s//sky/g) Replace all vivian in each line with sky
  3. You can use # as a separator, and the / that appears in the middle will not be used as a separator
    :s#vivian/#sky/# Replace the first vivian/ in the current line with sky/
  4. Delete ^M in the text
    Description of the problem: For line feed, it is represented by carriage return and line feed (0A0D) under Windows, and carriage return (0A) under Linux. In this way, when copying files from Windows to Unix, there will always be a ^M. Please write a shell or c program to filter the newline character (0D) of Windows files under Unix.
    Use the command: cat filename1 | tr -d “^V^M” >newfile;
    Use the command: sed -e “s/^V^M//” filename >outputfilename. It should be noted that in the two methods 1 and 2, ^V and ^M refer to Ctrl+V and Ctrl+M. You have to type it in by hand, not paste it.
    · Processing in vi: first use vi to open the file, then press the ESC key, and then enter the command: %s/^V^M//.
    · :%s/^M$//g
    If the above methods are useless, the correct solution is:
    · tr -d “r” dest
    · tr -d “5” dest
    · strings A>B
  5. Others
    Use the :s command to replace strings. Specific usage includes:
    :s/str1/str2/ replaces the first occurrence of the string str1 on the line with the string str2
    :s/str1/str2/g replace all occurrences of the string str1 in the line with the string str2
    :.,$ s/str1/str2/g Replace all occurrences of the string str1 from the current line to the end of the text with the string str2
    :1,$ s/str1/str2/g Replace all occurrences of the string str1 in the text with the string str2
    :g/str1/s//str2/g Same function as above
    It can be seen from the above replacement command: put g at the end of the command, which means to replace every occurrence of the search string; without g, means to only replace the search string
    The first occurrence of the string is replaced; g is placed at the beginning of the command, which means that all lines containing the search string in the text are replaced.

Leave a Reply

Your email address will not be published. Required fields are marked *