For example, you need to replace all abc files with deffind-typef | xargsperl-I-pes % abc % def % g. If you want to replace the contents of the subdirectories with ls-R | xargsperl -I-pes % abc % def % g programming today, for a small example of Stack, I need to set & rdquo; S. & rdquo; replace with & for example, 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 I write abc, I read xyz and abc., the result after replacement is I write xyz, I read xyz and abc.
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, this row S. abc and S-> bcd and S. aaa and S .. abc is replaced with S-> abc and S-> bcd and S-> aaa and S->. 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.php and http:/uw/faf
Use the following replacement command (# is used here #):
: S # http: // roclinux \. cn/index \. php # http: // www \. sohu \. com #
Replaced with: http://www.sohu.com and http:/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:
Aaa 2008-07-01 bbb
Ccc 2008-07-12 aaa
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!