SED beginner practical Description _linux Shell

Source: Internet
Author: User
Tags regular expression

For example, after more than one system, application installation, we often need to modify a lot of configuration files, with the VI editor means time-consuming, repetitive work, and SED can release us from the heavy duplication of work.

How SED is invoked:

1, sed [sed option] ' sed command ' to modify the file
2, sed [sed option]-f SED script to modify files
3, SED script [sed option] to modify the file

Here we only describe the first way to use the most

SED option: Only columns that are commonly used

-I: Directly modify the source file (without this option can not be directly modified, you must redirect to the new file, if only as an output test does not need this option), you can also modify the source files before the backup
Sed-i.bak ' s/123/234/' test.txt test.txt back to Test.txt.bak, then replaces the first occurrence of the file with "123" with "234"
-e: Edit multiple times, for example, replace all 123 with 234, and then add the # Comment before line 7th
Sed-i-E ' s/123/234/g '-i-e ' 7 s/^/#/' test.txt
-F: Specify sed script filename
-N: Cancels the default output (does not print), with SED without any options you will see all content output, not according to the SED command constraints of the conditional output, then with the "P" command can filter out the line that does not meet the criteria, and display the criteria of the line, for example
Sed-n ' finds all lines containing the character 123 ' P test.txt or sed-n ' to find all rows containing the character 123 P ' test.txt

SED command: You can use regular expressions here, such as special characters, you need to "\" to mask their special meaning, such as "\$" represents the ordinary character $

x x is a row number
X,y For example 2, 5, from line 2nd to line 5th
/val/query contains line with "Val" character
/val1/val2/query contains two-mode rows
Val/,x a row that contains "Val" on the line of a given line number
x,/val/matching rows by line number and pattern query
x,y! Rows that do not contain the specified line number X,y

P Print a matching row
= Show line number
A\ additional content after a positioned line
I\ inserts the content after the positioned line
D Delete the positioned row, for example: 2d represents the deletion of line 2nd
C\ Replace the text of the anchor line with new text
s replacement, in the form of: ' s/find content/replacement content/'
G for global substitution, if not using this option, replaces the first result that matches to the row, the next match is not processed.
... The other is not introduced, Baidu search down the bar

Example Learning:
Test.txt file content is
The Honeysuckle Band played all night long for only $90
It was a evening of splendid music and company
Too bad the disco floor fell through at 23:10
The local nurse Miss P.neave is in attendance

1, display the contents of line 2nd
Sed-n ' 2p ' test.txt

2, display the contents of line 1th to 3rd
Sed-n ' 1,3 ' p test.txt

3. Show only the rows containing "disco"
Sed-n '/disco/' P test.txt

4, display the line containing the "$" character
Sed-n '/\$/' P test.txt

5, display the line with the end of the number, [0-9] is a regular expression representing the number 0 to 9; "$" for the end of the line ("^" indicates the beginning of the row)
Sed-n '/[0-9]$/' P test.txt
Display results as:
The Honeysuckle Band played all night long for only $90
Too bad the disco floor fell through at 23:10

6, display the line with the end of the number and display the line with capital T as the beginning
Sed-n-E '/^t/' p-e '/[0-9]$/' P test.txt

7, matching any letter, followed by a number of arbitrary letters repeated, and "ing" the end of the line
Sed-n '/.*ing/' P test.txt

8, the first line and the last line
Sed-n ' 1p ' test.txt
Sed-n ' $p ' test.txt

9, change "Nurse" to "little Nurse", "&" command to recall the replaced content
Sed-n ' S/nurse/little &/p ' test.txt

10, first replace all 123 to 234, and then add the 7th line before the # Note
Sed-i-E ' s/123/234/g '-i-e ' 7 s/^/#/' test.txt

11, delete the "--------", delete the blank line, delete the first and last line, and print the first column
File contents:
Database Size (MB) Date Created
------------------------------------------
MySQL 2244 12/11/08
Test 5632 12/11/08

(2 rows affected)
Command:
Cat Test.txt | Sed ' s/--*//g ' | Sed '/^$/d ' | Sed ' $d ' | Sed ' 1d ' | awk ' {print '} '
Show Results:
Mysql
Test
Description
Use s/--*//g to remove horizontal lines-------
Use/^$/d to delete blank lines
Use $d to delete the last row
Delete the first line using 1d
awk {print} prints the first column

12. Some miscellaneous examples
^[0-9] indicates that the first character of the line is any number, such as "1ASDF";
^[0-9]* indicates that the line header contains any number of digits, such as "1818ASDF"
[0-9] [0-9]*$ indicates that the end of the line contains at least 2 digits, such as "Asdfasdf18" "asdf1818"
Sed-i ' s/^[0-9]*//g ' test.txt delete any number at the beginning of the line
Sed-i-E ' s/^[0-9]*//g '-i-e ' s/$/& passed/' test.txt deletes any number at the beginning of the line and adds "passed" at the end of each row
Sed-i-E ' s/^/#& ' Test.txt adds a "#" note to the beginning of each line
S/\.$//g to delete the period of a line ending with a period
s/^[] []*//Delete any space at the beginning of a line
s/^.//deletes the first character at the beginning of a line
s/^\///Delete the "/" character at the beginning of a line
S/sp\ (.. \)//g deletes the character "SP" and the two arbitrary characters immediately following it, "splly"--> "Y"

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.