Search and replace strings in Linux

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.