sed command: Stream editor
Workflow for SED commands:
sed copies one or more lines from the original file and acts on a line-by-row basis. First, the contents of the row are placed in the pattern space, and the bounds or regular expression matching operations are performed within the pattern space.
A. If the contents of the line do not conform to the regular expression or delimitation, then the content is judged as no for standard output.
B. If the line content conforms to a regular expression or delimitation, the content is judged to be Yes and edited (including standard output).
The contents of the mode space and the hold space in SED will be swapped, which will make the file content to be filtered and edited multiple times to complete complex tasks.
In general, SED will not change the contents of the original file, if necessary, you can add the-i parameter to change the contents of the original file. (It's a good idea to make a backup of the original file before changing the content)
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/85/wKioL1enIj_Si5KVAABe3p3gDYc097.png "title=" QQ picture 20160807195603.png "alt=" Wkiol1enij_si5kvaabe3p3gdyc097.png "/>
sed [OPTION] ... ' Script ' [Input-file] ...
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/85/wKioL1enMerjFSyzAAAtSsZ9Y0I451.png "title=" Sed.png "alt=" Wkiol1enmerjfsyzaaatssz9y0i451.png "/>
Script: Address delimitation Edit Command
Common options:
-N: Do not output the contents of the mode space to the screen
-E: Enable multi-point editing
-f/path/to/sed_script_file: one edit command per line
-R: Supports the use of extended regular expressions
-I: Edit the original file directly
Address delimitation:
No address: Full text processing (open site)
Single Address:
#: Specify Line
/pattern/: Each row that is matched by this pattern
Address range:
#,#: From the first line to the first line
#,/pattern/: Line starting from the specified line to the 1th pattern match
/par1/,/par2/: line to match from 1th par to 2nd par
$: Last line
Step in Address: ~
: All Odd lines
2~2: All even lines
Edit command:
D: delete example: sed ' 1,5d '/etc/fstab remove 1th to 5th lines from Fstab file
P: Display the contents of the mode space
A \text: Append text "text" after line, support \ n implement multiline append
I \text: Insert the text "text" in front of the line,
Cases:
[Email protected] ~]# sed ' 3i \newline '/etc/fstab #在第3行前面插入newline
#
NewLine
#/etc/fstab
# Created by Anaconda on Tue Jul 19 18:09:10 2016
[[Email protected] ~]# sed ' 3i \newline\nthis is anther line '/etc/fstab #支持 \ n
#
NewLine
This was anther line
#/etc/fstab
C \text: Replace the matched line with the text "text" specified here
Cases:
[Email protected] ~]# sed '/^uuid/c \ #new line '/etc/fstab #将所有UUID开头的行替换 For new Line
#
#/etc/fstab
# Created by Anaconda on Tue Jul 19 18:09:10 2016
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
#new Line
#new Line
#new Line
#new Line
TMPFS/DEV/SHM TMPFS Defaults 0 0
w/path/to/somefile/: Save the pattern space to match the line to the specified file
Cases:
[[Email protected] ~]# sed '/^[^#]/w/tmp/fstab.new '/etc/fstab #将非 # start line Save Into the/tmp/fstab.new.
[Email protected] ~]# cat/tmp/fstab.new
UUID=D805BCFE-F510-4569-8BB8-3EED373118EA/EXT4 Defaults 1 1
Uuid=150c09c2-f004-4878-985b-be8f475c60ee/boot EXT4 Defaults 1 2
Uuid=768a541e-43c9-446a-a84e-fd0a2edc9437/testdir EXT4 Defaults 1 2
Uuid=95011402-df68-4950-8584-23826c8d3a79 swap swap defaults 0 0
TMPFS/DEV/SHM TMPFS Defaults 0 0
Devpts/dev/pts devpts gid=5,mode=620 0 0
Sysfs/sys Sysfs Defaults 0 0
PROC/PROC proc Defaults 0 0
R/path/from/somefile: Reads the contents of the specified file after the line of the line to which the current file is matched by the pattern, implementing the file merge
Cases:
[Email protected] ~]# sed '/uuid/r/etc/issue '/etc/fstab #在所有UUID开头的行后面插 Into the/etc/issue file contents
#
#/etc/fstab
# Created by Anaconda on Tue Jul 19 18:09:10 2016
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
UUID=D805BCFE-F510-4569-8BB8-3EED373118EA/EXT4 Defaults 1 1
CentOS Release 6.8 (Final)
Kernel \ r on an \m
Uuid=150c09c2-f004-4878-985b-be8f475c60ee/boot EXT4 Defaults 1 2
CentOS Release 6.8 (Final)
Kernel \ r on an \m
Uuid=768a541e-43c9-446a-a84e-fd0a2edc9437/testdir EXT4 Defaults 1 2
CentOS Release 6.8 (Final)
Kernel \ r on an \m
Uuid=95011402-df68-4950-8584-23826c8d3a79 swap swap defaults 0 0
CentOS Release 6.8 (Final)
Kernel \ r on an \m
TMPFS/DEV/SHM TMPFS Defaults 0 0
=: Prints line number for the line to which the pattern matches
! : Conditional inversion (address delimitation!) Edit command)
Cases:
[[Email protected] ~]# sed '/^#/!d '/etc/fstab #删除fstab文件中不以 # lines beginning with
#
#/etc/fstab
# Created by Anaconda on Tue Jul 19 18:09:10 2016
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
[Email protected] ~]#
s///: Find replacement, its delimiter can be self-made, commonly used are [email protected]@@,s## #等.
Replace tag:
G: Global Substitution
W: Saves the result of the substitution success to the specified file
P: Show the rows that were successfully replaced
Cases:
Remove all white-space characters from the beginning of all lines in the/boot/grwb/grub2.cfg file that begin with a blank character
sed ' [email protected]^[[:space:]]\[email protected]@ '/etc/grub2.cfg #该/etc/grub2.cfg file on linux7 system
Delete all whitespace characters after # and # at the beginning of all lines beginning with # in the/etc/fstab file
[[Email protected] ~]# sed ' [email protected]^#[[:space:]]*@@ '/etc/fstab
Output an absolute path to the SED command, taking out its directory, which behaves like dirname
[Email protected] ~]# echo "/var/log/messages/" | Sed ' [Email protected][^/]\+/\[email protected]@ '
/var/log/
Advanced Editing Commands:
H: Overwrite the contents of the pattern space in the hold space
H: Append the contents of the pattern space to the hold space tail
G: Overlay content in the hold space into the pattern space
G: Append the contents of the hold space to the rear of the pattern space
x: Swap the content in the pattern space with the content in the hold space
N: Overwrite read match to row next line to pattern space (overwrite original row)
N: Append read match to row below line to pattern space
D: Delete rows in the pattern space
D: Delete all rows in multi-line mode space
Cases:
[Email protected] ~]# sed-n ' n;p '/etc/fstab #显示偶数行
#
# Created by Anaconda on Fri Jul 8 21:17:42 2016
# Accessible filesystems, by reference, is maintained under '/dev/disk '
#
Uuid=07a0c0c3-47db-4cc9-95e5-e5c7d415c126/boot XFS Defaults 0 0
[Email protected] ~]# sed ' $!d '/etc/fstab #取出最后一行
/dev/mapper/rhel-swap swap swap defaults 0 0
[[Email protected] ~]# sed ' $! n;$! D '/etc/fstab #取出最后两行
Uuid=07a0c0c3-47db-4cc9-95e5-e5c7d415c126/boot XFS Defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
[[Email protected] ~]# sed '/^$/d; G '/etc/fstab #删除原有的所有空白行, and then add a blank line after all the non-blank lines
#
#/etc/fstab
# Created by Anaconda on Fri Jul 8 21:17:42 2016
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
/DEV/MAPPER/RHEL-ROOT/XFS Defaults 0 0
Uuid=07a0c0c3-47db-4cc9-95e5-e5c7d415c126/boot XFS Defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
[Email protected] ~]# sed ' n;d '/etc/fstab #显示奇数行
#/etc/fstab
#
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
/DEV/MAPPER/RHEL-ROOT/XFS Defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
[Email protected] ~]# sed ' G '/etc/fstab #在每一行后面添加空白行
#
#/etc/fstab
# Created by Anaconda on Fri Jul 8 21:17:42 2016
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
/DEV/MAPPER/RHEL-ROOT/XFS Defaults 0 0
Uuid=07a0c0c3-47db-4cc9-95e5-e5c7d415c126/boot XFS Defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
This article is from the "zebra930" blog, make sure to keep this source http://zebra930.blog.51cto.com/11736340/1835444
Brief introduction to the SED command in Linux