The string in bulk replacement text in Linux--reproduced

Source: Internet
Author: User

(a) replaced by the VI editor.
You can use the: s command to replace a string in Vi/vim.
: S/well/good/replaces the current line with the first well being a good
: S/well/good/g replaces the current line all well is good
: N, $s/well/good/replaces the first well of each row in the beginning of the nth row to the last row good
: N, $s/well/good/g replace the nth line to the last row, and all well is good for each row
N is a number, if N is., indicating the beginning of the current line to the last row
:%s/well/good/(equivalent to: g/well/s//good/) replaces the first well of each row as good
:%s/well/good/g (equivalent to: g/well/s//good/g) replaces all the well in each row as good
You can use # as a delimiter, at which time the middle/not as a delimiter
: s#well/#good/# Replace the current row the first well/is good/
:%s#/usr/bin#/bin#g can change all the paths in the file/usr/bin to/bin

(b) Replace the string directly in the file. (This method replaces the string without opening the file, and can replace multiple files in bulk.) )
1.perl command replacement, the parameter meaning is as follows:
-A auto-delimited mode, separated by a space $_ and saved to @f. Equivalent to @f = Split ". The delimiter can be specified with the-f parameter
-F Specify a delimiter for-a, you can use regular expressions
-e executes the specified script.
-i< extension > Replace the file in place and back up the old file with the specified extension. Do not back up without specifying a name extension.
-L automatically chomp the input content and automatically adds line breaks to the output
-N auto-loop, equivalent to while (<>) {script;}
-P Auto Loop + Auto output, equivalent to while (<>) {script; print;}
Usage examples:
Perl-p-i.bak-e ' s/\bfoo\b/bar/g ' *.c
Replace Foo in all C programs with bar and old files back to. bak

Perl-p-i-e "s/shan/hua/g"./lishan.txt./lishan.txt.bak
Replace "Shan" in Lishan.txt and Lishan.txt.bak with "Hua" in the current folder

Perl-i.bak-pe ' s/(\d+)/1 + $1/ge ' file1 file2
Add a value to each file that appears in the

Bulk Replace file contents under 2.sed command

Format: Sed-i "s/lookup field/replacement field/g" grep lookup field-rl path ' file name

-I means inplace edit, in-place modify file

-R indicates a search sub-directory

-l indicates output matching file name
s means replace, d means delete

Example: Sed-i "s/shan/hua/g" Lishan.txt

Replace the Shan in the current directory with the Hua. lishan.txt

Other uses of SED are as follows:

1. Delete the beginning of the line space
Sed ' s/^[]*//g ' filename
Sed ' s/^ *//g ' filename
Sed ' s/^[[:space:]]*//g ' filename

2. Add new lines after line and before line
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 in last line
Sed-i ' $ a\ insert string ' filename

6. Insert before matching line
Sed-i '/pattern/i ' Insert string ' ' filename '

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

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

The string in bulk replacement text in Linux--reproduced

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.