LINUX vim replaces string instructions

  
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 (vi is really powerful, there is still a lot to learn), and several records are here to facilitate future queries. :s/vivian/sky/Replace the current line first vivian for sky :s/vivian/sky/g Replace the current line all vivian for sky :n, $s/vivian/sky/Replace the nth line from the beginning to the last line The first vivian of each line is sky :n, $s/vivian/sky/g replaces the nth line from the beginning to the last line. All vivians are sky n as numbers. If n is ., it means starting from the current line. The last line: %s/vivian/sky/(equivalent to: g/vivian/s//sky/) Replace the first vivian of each line with sky :%s/vivian/sky/g (equivalent to: g/vivian /s//sky/g) Replace all vivian in each line for sky. You can use # as the separator. The /in the middle will not be used as a separator: s#vivian/#sky/# replaces the current line first vivian /for sky/:%s+/oradata/apras/+/user01/apras1+ (replace with + /): /oradata/apras/replaced with /user01/apras1/1.:s/vivian/sky/replace current line A vivian replaces the current line for sky :s/vivian/sky/g All vivian is sky 2. :n,$s/vivian/sky/Replace the first vivian of each line from the beginning of the nth line to the last line is sky :n, $s/vivian/sky/g replace the nth line In the last line, all vivians are sky (n is a number, if n is ., it means starting from the current line to the last line) 3. :%s/vivian/sky/(equivalent to: g/vivian/s//Sky/) Replace the first vivian of each line as sky :%s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) Replace all vivian in each line for sky 4. Can use # As a separator, the /appearing in the middle does not act as a separator: s#vivian/#sky/# replaces the current line first vivian/is sky/5. deletes the ^M in the text Description: For line breaks, window Under the carriage return line (0A0D) to indicate that Linux is a carriage return (0A) to indicate. In this way, when copying the file on the window to Unix, there will always be a ^M. Please write a shell or c program for the line break (0D) of the filtered windows file under Unix. . Use the command: cat filename1 |  Tr -d “^V^M” > newfile; Use the command: sed -e “s/^V^M//” filename > outputfilename. Note that in the two methods 1, ^V and ^M refer to Ctrl+V and Ctrl+ M. You have to do the input manually, not the paste. . Handling in vi: first open the file with vi, then press the ESC key, then enter the command: %s/^V^M//. :%s/^M$//g If the above method is useless, the correct solution is: . Tr -d "\ " dest . Tr -d "\\015" dest . Strings A>B 6. Others The replacement of strings can be achieved with the :s command. The specific usages include: :s/str1/str2/Replace the first occurrence of the string in the line with the string str2 str1 :s/str1/str2/g Replace all occurrences of the string str1 : with the string str2 . $ s/str1/str2/g Replace the text with the string str2 to the end of the current string str1 :1, $ s/str1/str2/g Replace all occurrences of the string str1 in the body with the string str2 : g/str1/s//str2/g The function is the same as above. You can see from the above replacement command: g is placed at the end of the command, indicating that each occurrence of the search string is replaced; without g, it means only for the first time of the search string. Replacement occurs; g is placed at the beginning of the command to indicate that all rows in the body that contain the search string are replaced.
Copyright © Windows knowledge All Rights Reserved