Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use vim instruction to replace string in LINUX

Shulou Source: shulou.com Published: 2022-06-01 16:04:04 10月04日 Update

This article introduces how to use vim instruction to replace strings in LINUX. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The: s command can be used in vi/vim 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). Record a few here to facilitate future query.

: s/vivian/sky/ replaces the current line with sky as the first vivian

: s/vivian/sky/g replaces all vivian in the current line to sky

The first vivian of each line from the beginning of line n to the last line of replacement is sky

All vivian in each line from the beginning of the nth line to the last line is sky

N is a number. If n is., it starts from the current line to the last line.

:% s/vivian/sky/ (equivalent to: g/vivian/s//sky/) replaces the first vivian of each line with sky

:% s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) replaces all vivian in each line with sky

You can use # as the delimiter, and the / that appears in the middle will not be the delimiter

: s#vivian/#sky/# replaces the current line with sky/ as the first vivian/

:% oradata/apras/ oradata/apras/ aprasqpr apras1 + (replace with +): / replace with /): / replace with / apras1 +: / apras1 +

1.:s/vivian/sky/ replaces the current line with sky as the first vivian

: s/vivian/sky/g replaces all vivian in the current line to sky

2.: the first vivian of each line from the beginning of line n to the last line is sky

All vivian in each line from the beginning of the nth line to the last line is sky

(n is a number, if n is., from the current line to the last line)

3.:% s/vivian/sky/ (equivalent to: g/vivian/s//sky/) replaces the first vivian of each line with sky

:% s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) replaces all vivian in each line with sky

4. You can use # as the delimiter, and the / that appears in the middle will not be the delimiter

: s#vivian/#sky/# replaces the current line with sky/ as the first vivian/

5. Delete ^ M from text

Problem description: for line feeds, enter newline (0A0D) is used in window, and carriage return (0A) is used in linux. In this way, when you copy a file from window 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 methods 1 and 2, ^ V and ^ M refer to Ctrl+V and Ctrl+M. You have to type it by hand, not paste it.

. Processing in vi: first use vi to open the file, then press ESC, 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 "\ 015" dest

. Strings A > B

6. Other

You can use the: s command to replace a string. Specific uses include:

: s/str1/str2/ replaces the string str1 that first appears on the line with the string str2

S/str1/str2/g replaces all occurrences of the string str1 on the line with the string str2

: . , $s/str1/str2/g replaces the string str1 that appears from the current line to the end of the body with the string str2

: 1 s/str1/str2/g $s/str1/str2/g replaces all the strings str1 that appear in the body with the string str1

G/str1/s//str2/g function is the same as above

From the above replacement command, you can see that g is placed at the end of the command to replace each occurrence of the search string; without g, it means only the search

The first occurrence of the string is replaced; g is placed at the beginning of the command to replace all lines in the body that contain the search string.

On how to use the vim instruction in LINUX to replace the string is shared here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Character string line command delimiter file body search instruction content number method more end middle help input good powerful full text Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker OPPO Reno Microsoft Linux