I. INTRODUCTION of SED
Ii. sed syntax
Attached: regular expression meta-character
Iii. sed Common editing commands
Iv. Common options for SED
I. INTRODUCTION of SED
Three major Linux text processing tools, grep, sed, and awk.
Stream editor,sed is a text flow editor, which can edit the text line, use it to match the data to find, add, delete, replace and so on.
Ii. sed syntax
sed [Options]/pattern//path/to/file1 /path/to/file2 #pattern为匹配模式, separated by spaces when working with multiple files
Address delimitation/pattern/: Support for regular expressions, default support for basic regular expressions, extended regular expressions with option-r support
1. Can be just a pattern matching row
[Email protected] scripts]# sed '/^#/p '/scripts/test# the first line.# the first line. The second one.
2. Specify lines, such as 1,7 first line to line 7th, 3,/^#/line 3rd to #
[[email protected] scripts]# sed -n ' 1,3p;5,/^#/p ' /etc/fstab #打印文件中第1行到第3行, line 5th to the line that starts with the pound sign, its contents #第1行 # #第2行 # /etc/fstab #第3行 # #第4行 # accessible filesystems, by reference, are maintained under '/dev/disk ' #这里第5行和以井号开头的行是同一行
3. Find and Replace
[email protected] What to look for @ Replace with content @g: Find content can use pattern matching, replace with content cannot use pattern match, but can reference; flag bit: G for global, I for ignore case
[[email protected] scripts]# which lsalias ls= ' ls--color=auto '/bin/ls[[email protected] scripts]# which LS | Sed '/^alias/!s#^[[:space:]]## ' alias ls= ' ls--color=auto '/bin/ls[[email protected] scripts]# which LS | Sed '/^alias/d;s#^[[:space:]]## ' #先去掉alias开头的行 and then search for replacements to remove whitespace characters/bin/ls
Attached: regular expression meta-character
1, the basic regular expression
. Any single character
[] within range
[^] outside the range, such as non-whitespace characters [^[:space:]]
[0-9],[[:d IGI:]] Number
[A-z],[[:lower:]] Small Letter
[A-z],[[:upper:]] Uppercase
[A-za-z],[[:alpha:]] Letter
[A-za-z0-9],[[:alnum:]] numbers + letters
[[: Space:]] blank character
[[:p UNCT:]] punctuation marks
\{m,n\} at least m times, up to N times
\{m\} precise M-times
\{m,\} at least m times
\{0,n\} up to n times
* Match any character of the preceding character any time
\? Matches the preceding character 0 or 1 times
^ Anchor Beginning
$ Anchor Line End
\<,\b Anchor Word Head
\>,\b Anchor Ending
\ (\), \1, \2, Group
2. Extended Regular Expressions
. Any single character
[] within range
[^] outside the range, such as non-whitespace characters [^[:space:]]
[0-9],[[:d IGI:]] Number
[A-z],[[:lower:]] Small Letter
[A-z],[[:upper:]] Uppercase
[A-za-z],[[:alpha:]] Letter
[A-za-z0-9],[[:alnum:]] numbers + letters
[[: Space:]] blank character
[[:p UNCT:]] punctuation marks
{m,n} at least m times, up to N times
* Match any character of the preceding character any time
? Matches the preceding character 0 or 1 times
+ Match previous character 1 or more times
| Or
(), \1, \2 Group
^ Anchor Beginning
$ Anchor Line End
\<,\b Anchor Word Head
\>,\b Anchor Ending
Iii. sed Common editing commands
P: Print target text content and pattern-matching content on the screen
D: Delete the matched content and print the content to the screen
!d: Delete non-matching content and print the content to the screen
A \text: Add content to the next line of matching content, use \ n to add the next line
I \text: Add content to the front row of matching content
C \text: Replace the eligible content with the specified text
R/path/to/file: Add content to other files at matching content
W/path/to/file: Saving matches to a specified file
=: Prints the line number matching to the content
------------------------------------------
P: Print target text content and pattern-matching content on the screen
[email protected] scripts]# Cat test # The first line. The second one. [[Email protected] scripts]# sed/^#/p/scripts/test #未使用-N, not only print the match, all the contents of the file will also be printed to the screen # The first line.# the first line. The second one.
d: Delete the matched content and print the content to the screen
[email protected] scripts]# cat test# the first line. The second one. [Email protected] scripts]# sed/^#/d/scripts/test #删除以井号开头的行 the second one.
!d: Delete non-matching content and print the content to the screen
[email protected] scripts]# cat test# the first line. The second one. [Email protected] scripts]# sed '/^#/!d '/scripts/test #删除非井号开头的行 # the first line.
a \text: add content to the next line of matching content, use \ n to add the next line
[email protected] scripts]# cat test# the first line. The second one. [Email protected] scripts]# sed '/^#/a \next line '/scripts/test #内容插入到井号开头的行下一行 # The first line.next line the second One.
I \text: Add content to the front row of matching content
[email protected] scripts]# cat test# the first line. The second one. [Email protected] scripts]# sed '/^#/i \previous line '/scripts/test #内容插入到井号开头行的上一行previous line# the first line. The second one. You have new mail in/var/spool/mail/root
R /path/to/somefile: add content to other files at matching content
[email protected] scripts]# cat test# the first line. The second one. [Email protected] scripts]# sed '/^#/r/etc/issue '/scripts/test #把issue文件的内容加入匹配内容后 # the first line. CentOS Release 6.5 (Final) #issue文件中内容Kernel \ r on \m #issue文件中内容 #issue文件中内容 the second one .
c \text: Replace matched content with specified text
[Email protected] scripts]#!ccat test# the first line. The second one. [Email protected] scripts]# sed '/^#/c \replace line. '/scripts/test #替换内容Replace line. The second one.
W /path/to/file: Save the match to the specified file
[Email protected] scripts]# lstest[[email protected] scripts]# sed '/^#/w/scripts/new '/scripts/test# the first line. The second one. [[email protected] scripts]# cat new #匹配的内容已经添加到new文件中, tested if content exists in the file, this command is equivalent to > output redirection instead of >> append # the first line.
=: Prints the line number matching to the content
[email protected] scripts]# cat test# The first line.# the second one.# last line.111111111111111222222222222222333333333 333333# New Line. [Email protected] scripts]# sed/^#/=/scripts/test #匹配到内容所在行的行号1 # The first line.2# the second one.3# last line.11111 11111111112222222222222223333333333333337# New Line.
Iv. Common options for SED
-N: Silent mode, only the screen prints out the pattern matching content
-E: Use multiple/pattern/in an sed command, such as sed–e/pattern1/–e/pattern2/–e/pattern3/ /path/to/file1/path/to/file2, for example
-I: Modify file contents directly
-R: Using extended regular expression meta-characters
-F: Reads the processing script from the file and executes; sed-f sed_scripts8/path/to/file1/path/to/file2
------------------------------------------
- n: Silent mode, only the screen prints out the pattern matching content
[email protected] scripts]# cat test# The first line.# the second line.# last line. [[email protected] scripts]# sed/first/p/scripts/test #默认除了打印匹配的内容, the contents of the file will also be printed to the screen # The first line.# the first line.# the Second line.# last line. [Email protected] scripts]# sed-n/first/p/scripts/test #仅打印匹配的内容 # the first line. [Email protected] scripts]#
- e: Use multiple/pattern/in an sed command
[[email protected] scripts]# which lsalias ls= ' ls--color=auto '/bin/ls[[email protected] scripts]# which LS | Sed '/^alias/d '/bin/ls[[email protected] scripts]# which LS | Sed-e '/^alias/d '-e s#^[[:space:]]## #使用多个/pattern//bin/ls [[email protected] scripts]#
-I: Modify file contents directly
[email protected] scripts]# cat test# the first line. The second line. Last line. [Email protected] scripts]# sed-i ' s/the first line./the new line./'/scripts/test #将文件中 "the first line." Modified to "The new line." [email protected] scripts]# cat Test #已被修改 # The new line. The second line. Last line.
- F: Reads the processing script from the file and executes; sed-f sed_scripts8/path/to/file1/path/to/file2
[[email protected] scripts]# lsnew sed.script1 test[[email protected] scripts]# cat sed.script1 #创建一个脚本, each line is a matching mode or life Make/^#/p/second/w/scripts/new2[[email protected] scripts]# sed-f/scripts/sed.script1/scripts/test #使用脚本文件执行命令 # The New line.# the new line. The second line. Last line. [[email protected] scripts]# lsnew new2 sed.script1 test[[email protected] scripts]# cat New2 #第二条命令执行结果---> Success Th e second line.
This article is from the "Arvin Lau" blog, be sure to keep this source http://64314491.blog.51cto.com/2784219/1639404
Sed for Linux