For example, you need to replace abc in all abc files with def.
Find-type f | xargs perl-I-pe s % abc % def % g
If you want to replace the contents of the subdirectory together
Ls-R | xargs perl-I-pe s % abc % def % g
Today's programming, a small example of stack, in which I need to replace "S." with "S->" (replacement does not include double quotation marks ).
In fact, this is not difficult, but I think we should summarize the Replacement Technology in vi for future reference.
1, $ s, \ $ (M. * trans/np/, g
1, $ indicates the line from the beginning to the end, s indicates the replacement, represents the replacement symbol, \ $ (M. * trans/np/indicates the guy who wants to step down, for example, $ (Myg) odtrans/np/, indicates the guy who wants to go on stage. This is empty, g indicates that all the items found in this row are replaced.
1, $ s, \ $. * cmd/, g
In the full text, whenever you encounter $ sdkfcmd/such eggs, all are replaced with empty bags.
1
All replacement schemes must be written in the colon ":" state.
2
If you want to replace abc with xyz
: S/abc/xyz/
Note that this replacement is only limited to the current row where the cursor is located, and worse, it only replaces the first match of the current row.
That is, if the content of this row is Iwriteabc and Ireadxyzandabc, the result after replacement is Iwritexyz and Ireadxyzandabc.
3
If you want to replace all the matches in a row, it will be like this:
: S/abc/xyz/g
4
If you want to replace all the "S." in a row with "S->", you can do this:
: S/S \./S->/g
For example, replace S. abcandS-> bcdandS. aaaandS... abc with S-> abcandS-> bcdandS-> aaaandS->. abc.
Because the period "." is a special match character, if you want to match the period, you need to use \ before the period to cancel the special meaning of the period.
5
To expand the replacement range, you can use the number of rows to limit:
If you want to replace the Full Text S. with S->, then:
: 1, $ s/S \./S->/g
We can see that m is added before s, and n indicates the row range. 1 indicates the first row, and $ indicates the last row. Of course, you can specify other rows.
Here, m or n can be represented by special symbols "." or "$", "." represents the current row, and "$" represents the last row.
If it is determined to replace the full text, % can be used to replace 1, $, namely: % s/S \./S->/g
6
You may find that if I want to match an http://roclinux.cn/index.php, it will be very annoying because of the fact that both the symbol and the symbol will make the replacement messy. At this time, you can use # or any other symbol to replace/. For example, s/abc/xyz/can be replaced with s # abc # xyz # Or s! Abc! Xyz! Yes! This is amazing. If you know perl, you will find more flexible skills. Haha
The line content is: http://roclinux.cn/index.phpandhttp:/uuw/faf
Use the following replacement command (# is used here #):
: S # http: // roclinux \. cn/index \. php # http: // www \. sohu \. com #
Replaced with: http://www.sohu.comandhttp:/uuw/faf
7
If I want to add a pair of single quotes on both sides of a string with a date format (XXXX-XX-XX), that is, 2008 to '2017-07-01 ', I can do this.
For example, the original file content is:
Aaa2008-07-01bbb
Ccc2008-07-12aaa
Use the following replacement command:
: 1, $ s/\ ([0-9] \ {4 \}-[0-9] \ {2 \}-[0-9] \ {2 \}\) /'\ 1'/g
After replacement:
Aaa' 2008-07-01 'bbb
Ccc '2017-07-12' aaa
This replacement command is complex and contains the following knowledge points:
First knowledge point: [0-9] indicates any number between 0 and 9.
The second knowledge point: \ {n \} indicates that the element before the symbol is repeated n times, so [0-9] \ {4 \} indicates a four-digit number.
Third knowledge point: if \ (\) is used in the matching scheme, it indicates that the matching content needs to be saved, and \ 1 indicates replacing it with the temporary Matching content, take a closer look at the replacement command and you will understand it. If we match, we will be saved after brackets are added, at the end of \ 1, it will be replaced, and a pair of quotation marks will be added to the outside. That's OK!