SED powerful text Processing tool with a working flow of roughly the following
Common Options Description
-e<script> or--expression=<script> processes the input text file with the script specified in the options.
-f<script file > or--file=<script file > process the input text file with the script file specified in the options.
-h or--help display Help.
-N or--quiet or--silent only show results after script processing.
-V or--version displays version information.
Operation Command:
A: New, a can be followed by a string, and these strings will appear in a new line (the current next line) ~
C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!
D: Delete, because it is deleted ah, so d usually do not pick up any boom;
I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line);
P: Printing, that is, printing a selected data. Normally p will run with the parameter Sed-n ~
S: Replace, can be directly replaced by work! Usually this s action can be paired with formal notation.
1. Experimental environment
[[email protected] test]# pwd/root/test[[email protected] test]# lsfstab yum.conf[[email protected] test]# cat -n fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Sun Apr 22 06:26:44 2018 5 # 6 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 10 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 11 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Pattern Delete:
#删除1到9行 [[email protected] test]# sed ' 1,9d ' Fstabuuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot x FS defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0#-- ------------------------------------------------------------------------------------------------#删除第3行 [[Email protected] test]# sed ' 3d ' fstab## Created by Anaconda on Sun April 06:26:44 2018## Accessible filesystems, by Refe Rence, is maintained under '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info#uuid=7c EB028A-A8B8-467C-B6D4-36910C06C5AC/XFS Defaults 0 0uuid=3d81b92c-abeb-41f5-8de0-b46d3ff Bcf4c/boot XFS defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf93693f6b Swap Swap defaults 0 0#--------------------------------------------------------------------------------------------- -----#删除从第3行开始 5 consecutive rows of data [[EMAIL&NBSP;PRotected] test]# sed ' 3,+5d ' fstab#uuid=7ceb028a-a8b8-467c-b6d4-36910c06c5ac/xfs defaults 0 0uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot xfs Defaults 0 0uuid=943c7e04-b733-42fe- A1E2-EABF93693F6B Swap swap defaults 0 0#---------------------------------------------------- ----------------------------------------------#删除奇数行 [[[email protected] test]# sed ' 1~2d ' fstab## Created by Anaconda on Sun April 06:26:44 2018# Accessible filesystems, by reference, is maintained under '/dev/disk ' #UUID =3d81b92c -abeb-41f5-8de0-b46d3ffbcf4c/boot xfs defaults 0 0#----------------------------------------- ---------------------------------------------------------#删除 ^uuid=7 start line to uuid=9 the first occurrence of the row if Uuid=9 does not appear, then continue to delete [[ email protected] test]# sed '/^uuid=7/,/^uuid=9/d ' fstab##/etc/fstab# Created by Anaconda on Sun APR 22 06:26:44 201 8## 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##---------------- ----------------------------------------------------------------------------------#删除第5行开始的行 to uuid=9 the first occurrence of a row If Uuid=9 does not appear, then continue to delete [[email protected] test]# sed ' 5,/^uuid=999/d ' fstab##/etc/fstab# Created by Anaconda on Sun APR 06:26:44 2018#----------------------------------------------------------------------------------------------- ---#执行两次操作 [[email protected] test]# sed-e ' 1,3d '-e ' 4,6d ' fstab# See mans pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) For more INFO#UUID=7CEB028A-A8B8-467C-B6D4-36910C06C5AC/XFS defaults 0 0uuid= 3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot XFS Defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf9 3693F6B Swap swap defaults 0 0#-------------------------------------------------------------- ------------------------------------#执行多次操作 [[Email protected] test]# sed ' 1d;2d;3d ' fstab# Created by Anaconda on Sun Apr 06:26:44 2018## Accessible Filesys TEMs, by reference, is maintained under '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) For more INFO#UUID=7CEB028A-A8B8-467C-B6D4-36910C06C5AC/XFS Defaults 0 0uuid=3d81b92c-abeb-41f5 -8de0-b46d3ffbcf4c/boot XFS defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf93693f6b Swap Swap Defaults 0 0
Print
#删除第5行开始的行 to uuid=9 the first occurrence of the row to print once, the rest of the normal output [[email protected] test]# sed ' 5,/^uuid=999/p ' fstab##/etc/fstab# Created by Anaconda on Sun April 06:26:44 2018### Accessible filesystems, by reference, is maintained under '/dev/disk ' # ACCESSIBL e filesystems, by reference, is maintained under '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) For more info# See mans pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) For more info# #UUID =7ceb028a-a8b8-467c-b6d4-36 910C06C5AC/XFS defaults 0 0uuid=7ceb028a-a8b8-467c-b6d4-36910c06c5ac/ XFS defaults 0 0uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot xfs Defaults 0 0uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot xfs Defaults 0 0uuid=943c7e04-b733-42fe-a 1E2-EABF93693F6B Swap swap defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf93693f6b swap Swap DefauLTS 0 0#--------------------------------------------------------------------------------------------------# Delete line starting at line 5th to uuid=9 the first occurrence of the line look at the diagram to understand that the printed line executes first, and then all the data is processed in silent mode [[email protected] test]# sed-n ' 5,/^uuid=999/p ' fstab## Acc Essible filesystems, by reference, is maintained under '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or BL Kid (8) For more INFO#UUID=7CEB028A-A8B8-467C-B6D4-36910C06C5AC/XFS defaults 0 0uuid=3d8 1b92c-abeb-41f5-8de0-b46d3ffbcf4c/boot XFS Defaults 0 0uuid=943c7e04-b733-42fe-a1e2-eabf9369 3F6B swap swap defaults 0 0
Insert after first line
[[email protected] test]# sed ‘1a \newline‘ fstabnewline## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Third row insert
[[email protected] test]# sed ‘3i \newline‘ fstabnewline## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
The third line replaces
[[email protected] test]# sed ‘3c \newline‘ fstab#newline# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Write line fourth to the W.txt file
[[email protected] test]# sed ‘4w /root/test/w.txt‘ fstab## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0[[email protected] test]# cat w.txt# Created by anaconda on Sun Apr 22 06:26:44 2018
Read the file contents to line sixth below output
[[email protected] test]# sed ‘6r /root/test/w.txt‘ fstab## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# Created by anaconda on Sun Apr 22 06:26:44 2018# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Print line numbers for lines to which the pattern matches
[[email protected] test]# sed ‘6=‘ fstab## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018#6# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Regular use
[[email protected] test]# sed ‘1,10s/UUID/uuid/‘ fstab## /etc/fstab# Created by anaconda on Sun Apr 22 06:26:44 2018## Accessible filesystems, by reference, are maintained under ‘/dev/disk‘# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#uuid=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Advanced usage:
Reverse order
[[email protected] test]# sed ‘1!G;h;$!d‘ fstabUUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0## See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘## Created by anaconda on Sun Apr 22 06:26:44 2018# /etc/fstab#
Implement process PY Analysis
mode_space = ‘‘keep_space = ‘‘with open(‘test2.txt‘,encoding=‘utf-8‘) as f: i=0 for line in f: if i == 0: mode_space = line keep_space = mode_space else: mode_space = line+keep_space keep_space = mode_space i += 1 else: #最后一行读完删除 print(keep_space) del keep_space
Remove last line
[[email protected] test]# sed ‘$!d‘ fstabUUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Two lines after removing the file
[[email protected] test]# sed ‘$!N;$!D‘ fstabUUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Show even rows
[[email protected] test]# sed -n ‘n;p‘ fstab## Created by anaconda on Sun Apr 22 06:26:44 2018# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘#UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
Show Odd lines
[[email protected] test]# sed ‘n;d‘ fstab# /etc/fstab## See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more infoUUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
The SED for Linux text operations