Example of bulk find replacement text content in Linux

Source: Internet
Author: User

. Find the contents of a file in a batch, for example:

# grep-rn "Text to find".

2. Find and replace the contents of the file in bulk.

# sed-i "s/the text to find/replace text/g" ' Grep-rl "to find text"./'


other uses of SED are as follows:

1, delete the beginning of the space
Sed ' s/^[]*//g ' filename
Sed ' s/^ *//g ' filename
Sed ' s/^[[:space:]]*//g ' filename

2. Add new lines before and after lines
After line: sed ' s/pattern/&\n/g ' filename
Before line: sed ' s/pattern/\n&/g ' filename
& Representative Pattern

3. Use variable substitution (using double quotes)
Sed-e "s/$var 1/$var 2/g" filename

4. Insert text before the first line
Sed-i ' 1 i\ insert string ' filename

5, insert on the last line
Sed-i ' $ a\ insert string ' filename

6, insert before matching rows
Sed-i '/pattern/i insert string ' filename

7. Insert after matching line
Sed-i '/pattern/a insert string ' filename

8. Delete lines of empty text lines and spaces and the lines of # notes
Grep-v ^# FileName | Sed/^[[:space:]]*$/d | Sed/^$/d


Find Command


Find-name ' file name to find ' | Xargs perl-pi-e ' s| replaced string | replaced string |g '

#查找替换当前目录下包含字符串并进行替换

Find-name ' *.txt ' | Xargs perl-pi-e ' s| Wisdom Village | North Mountain |g '
#递归查找替换
Find. -type f-name ' *.html ' | Xargs perl-pi-e ' s| Wisdom Village | North Mountain |g '
#搜索目录LOG包含显示出来
Find./-name ' *log* '-type D
Find./-name ' *log* '-type d more

Of course, you can also use Linux vim bulk replacement


Replace only the current line:

Example

: s/xxx/yyy/g
, if you need to make all the substitutions:

Example

:%s/xxx/yyy/g
If you need to replace the specified section, you can enter visual mode with V, and then

Example

: s/xxx/yyy/g
Alternatively, you can specify the number of rows to replace the specified range:

Example

: 10,31s/xxx/yyy/g
If you need to display line number, under Vim

Example

: Set Nu
Suppress line number:

Example

: Set Nonu


: S/well/good/replaces the current line first good
: S/well/good/g replaces the current line all good
: N, $s/well/good/replaces the first n line to the top of each line in the last line good
: N, $s/well/good/g replaces the first n line to all the good in the last line of each line
N is a number, and if N is., it means starting from the current line to the last line
:%s/well/good/(equivalent to: g/well/s//good/) replaces the first good of each line
:%s/well/good/g (equivalent to: g/well/s//good/g) replaces all the good in each line
You can use # as a separator, in which the middle appears/is not a separator
: s#well/#good/# Replace the first well/of the current line as good/
:%s#/usr/bin#/bin#g can replace all the paths/usr/bin in the file with/bin

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.