Linux sed Bulk replacement strings and more usage __linux

Source: Internet
Author: User

For example, to modify the Zhangsan in all files below the directory/modules to Lisi, do this:

# sed-i "s/zhangsan/lisi/g" ' grep zhangsan-rl/modules '

Explanation:-I means inplace edit, in-place modification file-R, which indicates that the search subdirectory-L represents the output matching filename

This combination of commands is powerful and you need to be aware of backing up files.

# sed ' y/1234567890/abcdefghij/' test_sed
# sed ' y/1234567890/abcdefghij/' filename

Abcdefghij
Bcdefghija
Cdefghijab
Defghijabc

Note that the transformation relationship is based on the position of the two list corresponding to the transformation

Among them: The contents of test_sed are:

1234567890
2345678901
3456789012
4567890123

Replace all matches for each line

# sed ' s/01/ab/g ' test_sed

1234567890
23456789Ab
3456789ab2
456789ab23

Note: The first line of 0,1 is not replaced with a,b

Delete: D command

# sed ' 2d ' example deletes the second line of the example file.
# sed ' 2, $d ' example deletes the second line of example file to all rows at the end.
# sed ' $d ' example deletes the last line of the example file.
# sed '/test/' d example delete all rows containing test in the example file.

Replace: s command

# sed ' s/test/mytest/g ' example replaces test with mytest throughout the range. If there is no G-tag, only the first matching test for each row is replaced with mytest.
# sed-n ' s/^test/mytest/p ' Example (-N) option is used with the P flag to print only those lines that have been replaced. In other words, if test at the beginning of a line is replaced with mytest, it is printed.
# sed ' s/^192.168.0.1/&localhost/' example & symbol represents the part found in the replacement string. All rows beginning with 192.168.0.1 are replaced with localhost, which becomes 192.168.0.1localhost.
# sed-n ' s/able/1rs/p ' Example love is labeled 1, all loveable are replaced with lovers, and the replaced rows are printed.
# sed ' s#10#100#g ' example no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the separator, instead of the default "/" separator. means to replace all 10 with 100.

Range of selected rows: comma

# sed-n '/test/,/check/p ' example all rows in the range defined by the template test and check are printed.
# sed-n ' 5,/^test/p ' example print all rows from line fifth to the first line that contains the start of test.
# sed '/test/,/check/s/$/sed test/' example for the rows between the template test and West, the end of each line is replaced with the string sed test.

Multi-point edit: E command

# sed-e ' 1,5d '-e ' s/test/check/' example (-e) option allows multiple commands to be executed on the same line. As the example shows, the first command deletes 1 to 5 rows, and the second command replaces test with check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.
# sed–expression= ' s/test/check/' –expression= '/love/d ' example a better command than-E is –expression. It can assign a value to a SED expression.

Read from File: R command

# The contents of the SED '/test/r file ' example file are read in, displayed after the row that matches the test, and if multiple rows are matched, the contents of file are displayed below all matching rows.

Write to file: w command

# sed-n '/test/w file ' example all rows containing test in example are written to file.

Append command: A command

# sed '/^test/a\->this is a example ' example< ' is a example ' that is appended to the line beginning with test, SED requires that a backslash be followed by command a.

Insert: I command

$ sed '/test/i\
New Line
————————-' Example

If test is matched, the text that follows the backslash is inserted before the matching row.

Next: N command

# sed '/test/{n; s/aa/bb/} ' example if test is matched, move to the next line of the matching row, replace the AA of the line, change to BB, print the line, and then continue.

Variants: Y command

# sed ' 1,10y/abcde/abcde/' example converts all abcde in the 1–10 line to uppercase, noting that regular expression metacharacters cannot use this command.

Exit: Q command

# sed ' 10q ' example out of sed after printing line 10th.

Keep and get: H command and G command

# sed-e '/test/h '-e ' $Gexample

When SED processes files, each row is saved in a temporary buffer called the pattern space, and all rows processed are printed on the screen unless the row is deleted or the output is canceled. The mode space is then emptied and stored in a new line waiting to be processed. In this example, when the row that matches the test is found, it is stored in the mode space, and the H command copies it and stores it in a special buffer called the retention buffer. The second statement means that when the last row is reached, the G command takes out the line that holds the buffer and then puts it back in the pattern space and appends it to the end of the line that already exists in the pattern space. In this case, append to the last line. In simple terms, any row containing test is copied and appended to the end of the file.

Keep and swap: H command and x command

# sed-e '/test/h ' e '/check/x ' example

Swap the mode space and keep the contents of the buffer. That is, the line that contains test and check is swapped.

Script

The SED script is a list of SED commands that boot the script file name with the-F option when SED starts. Sed is very picky about the commands entered in the script and cannot have any white space or text at the end of the command, separated by semicolons if there are multiple commands on one line. Actions that begin with # comment on rows and cannot span rows.

Small Tips

Use double quotes instead of the usual single quotes when referencing shell variables in the SED command line. The following is a script that deletes the zone segment in the named.conf file based on the contents of the name variable:

Name= ' zone ' localhost ' sed '/$name/,/};/d ' named.conf sed-i ' s/oldstring/newstring/g '
grep oldstring-rl Yourdir '

For example: Replace www.itbbs.cn in all files under/home as chinafar.com

# sed-i "s/www.itbbs.cn/chinafar.com/g" ' grep www.itbbs.cn-rl/home '

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.