1. Find and replace methods used in VI
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/G is the same as 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, it indicates that only the first appearance of the search string is replaced; G is placed at the beginning of the command, indicating that all rows containing the search string in the body are replaced.
2.ShellUse Find and grep to replace files.
# Find./-exec grep str1 '{}'/;-exec sed-I. Bak S/str1/str2/G '{}'/;
The above command can search for files containing str1 in the current directory (including subdirectories), automatically replace it with str2, and generate the Bak file of the source file
3. Use the SED command to replace strings in multiple files in batches.
Sed-I "s/original string/New String/g" 'grep original string-rl directory'
Grep-LR "form.html"./| xargs sed-I "s/form.html/boot_form.html/G"
Share:
1. Find and replace methods used in VI
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/G is the same as 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, it indicates that only the first appearance of the search string is replaced; G is placed at the beginning of the command, indicating that all rows containing the search string in the body are replaced.
2.ShellUse Find and grep to replace files.
# Find./-exec grep str1 '{}'/;-exec sed-I. Bak S/str1/str2/G '{}'/;
The above command can search for files containing str1 in the current directory (including subdirectories), automatically replace it with str2, and generate the Bak file of the source file
3. Use the SED command to replace strings in multiple files in batches.
Sed-I "s/original string/New String/g" 'grep original string-rl directory'
Grep-LR "form.html"./| xargs sed-I "s/form.html/boot_form.html/G"