The use of the SED tool---------Linux

Source: Internet
Author: User

Sed

stream editor, line editor


how SED works

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/42/wKiom1m1D9fhB8YHAABO5xoQ2AI293.png "title=" Qq20170910181051.png "alt=" Wkiom1m1d9fhb8yhaabo5xoq2ai293.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/42/wKiom1m1D0WT1cMGAAA4IbtEK8c252.png "style=" float : none; "title=" Qq20170910180715.png "alt=" Wkiom1m1d0wt1cmgaaa4ibtek8c252.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/A4/F3/wKioL1m1DyDhOOpgAAA6IpFSLLY774.png "style=" float : none; "title=" Qq20170910180732.png "alt=" Wkiol1m1dydhoopgaaa6ipfslly774.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/06/42/wKiom1m1D0bz56khAAA5h7nf7gs984.png "style=" float : none; "title=" Qq20170910180741.png "alt=" Wkiom1m1d0bz56khaaa5h7nf7gs984.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A4/F3/wKioL1m1DyHjJ_PzAAA76jGBRSw451.png "style=" float : none; "title=" Qq20170910180749.png "alt=" Wkiol1m1dyhjj_pzaaa76jgbrsw451.png "/>


SED basic use

format one :

sed [OPTION ...] [SCRIPT] [FILE ...] Multiple file processing: One is processed and the second is processed. SED script file1--sed script file2 use basic with vim command 1) Vim%--sed no indication 2) VIM @@ ##/--sed | | ,, @@ //

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/42/wKiom1m1EIDDM-C0AABRJqCgFs8555.png "title=" Qq20170910181341.png "alt=" Wkiom1m1eiddm-c0aabrjqcgfs8555.png "/>


format Two :

Sed ' address-bound command ' FILE ...

1) Address delimitation

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/06/42/wKiom1m1FCmwPFsfAAAS-s6glds462.png "style=" float : none; "title=" Qq20170910182807.png "alt=" Wkiom1m1fcmwpfsfaaas-s6glds462.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/42/wKiom1m1FCnDz2VcAAAQ2l7GkpQ243.png "style=" float : none; "title=" Qq20170910182814.png "alt=" Wkiom1m1fcndz2vcaaaq2l7gkpq243.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A4/F3/wKioL1m1FAWDyDsJAAAcT-2ncug884.png "style=" float : none; "title=" Qq20170910182821.png "alt=" Wkiol1m1fawdydsjaaact-2ncug884.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/06/42/wKiom1m1FCqxibc4AABIcFEtJng420.png "style=" float : none; "title=" Qq20170910182829.png "alt=" Wkiom1m1fcqxibc4aabicfetjng420.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/F3/wKioL1m1FAWhqAulAAAmkPfBdCs908.png "style=" float : none; "title=" Qq20170910182835.png "alt=" Wkiol1m1fawhqaulaaamkpfbdcs908.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/42/wKiom1m1FCrj38PWAAAnjMB3nwk769.png "style=" float : none; "title=" Qq20170910182913.png "alt=" Wkiom1m1fcrj38pwaaanjmb3nwk769.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A4/F3/wKioL1m1FAaiH3SRAAAYpujmRoU388.png "style=" float : none; "title=" Qq20170910182919.png "alt=" Wkiol1m1faaih3sraaaypujmrou388.png "/>



2) command

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/42/wKiom1m1GxvgsRYDAAAjC3I5CMM740.png "style=" float : none; "title=" Qq20170910185709.png "alt=" Wkiom1m1gxvgsrydaaajc3i5cmm740.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/A4/F3/wKioL1m1GvewFoHNAAAiT3zBrZs856.png "style=" float : none; "title=" Qq20170910185718.png "alt=" Wkiol1m1gvewfohnaaait3zbrzs856.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/F3/wKioL1m1GvezJgVpAAA_mwnwomE635.png "style=" float : none; "title=" Qq20170910185725.png "alt=" Wkiol1m1gvezjgvpaaa_mwnwome635.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/42/wKiom1m1GxyxqhKVAAA4VZHD39Q626.png "style=" float : none; "title=" Qq20170910185732.png "alt=" Wkiom1m1gxyxqhkvaaa4vzhd39q626.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/42/wKiom1m1GxyjuvelAAA3uTPTbEE163.png "style=" float : none; "title=" Qq20170910185739.png "alt=" Wkiom1m1gxyjuvelaaa3utptbee163.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A4/F3/wKioL1m1GvjCAFupAAAlIsCagqo525.png "style=" float : none; "title=" Qq20170910185747.png "alt=" Wkiol1m1gvjcafupaaaliscagqo525.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/F3/wKioL1m1HJLR1LowAAARKHF2MOA545.png "title=" Qq20170910190548.png "alt=" Wkiol1m1hjlr1lowaaarkhf2moa545.png "/>

Example: inserting the contents of a file into a specified file # sed '/^uuid/r/etc/issue '/etc/fstab# sed ' 1,3r/etc/centos-release '/etc/issue

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A4/F3/wKioL1m1GvjB647jAAAb2YTbrgY507.png "style=" float : none; "title=" Qq20170910185803.png "alt=" Wkiol1m1gvjb647jaaab2ytbrgy507.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/42/wKiom1m1HOfBr6a7AAAr0UmdVJc700.png "title=" Qq20170910190637.png "alt=" Wkiom1m1hofbr6a7aaar0umdvjc700.png "/>

Example: Take a file base name, directory name: echo "/etc/sysconfig/useradd/" | Sed ' s,/$,, ' | Sed-r ' s| (.*/) ([^/]+) |\2| ' directory name: echo "/etc/sysconfig/useradd/" | Sed ' s,/$,, ' | Sed-r ' s| (.*/) ([^/]+) |\2| '

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/42/wKiom1m1HR3w0pF0AAA9iq8lXsU376.png "title=" Qq20170910190731.png "alt=" Wkiom1m1hr3w0pf0aaa9iq8lxsu376.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A4/F3/wKioL1m1HQ3x8FguAAAJG1GB_hE836.png "title=" Qq20170910190754.png "alt=" Wkiol1m1hq3x8fguaaajg1gb_he836.png "/>

Example: # echo "How is You" | Sed-r ' s/(How) (. *) [You]/\3\2\1/' # echo ' How is ' | Sed-r ' [email protected] (how) (. *) (you) @\3\2\[email protected] ' # echo ' How is ' | Sed-r ' s# (How) (. *) [You] #\3\2\1# ' # echo ' How is ' | Sed-r ' s, (How) (. *), \3\2\1, ' # echo ' How is ' | Sed-r ' s| (how) (.*) (You) |\3\2\1| '

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A4/F3/wKioL1m1HVKjQbayAAAOzJmq1tg687.png "style=" float : none; "title=" Qq20170910190837.png "alt=" Wkiol1m1hvkjqbayaaaozjmq1tg687.png "/>

For example: [[email protected] bin]# head-n 1 </etc/passwd | Sed ' s,root,root, ' root:x:0:0:root:/root:/bin/bash[[email protected ' bin]# head-n 1 </etc/passwd | Sed ' s,root,root,g ' root:x:0:0:root:/root:/bin/bash

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/42/wKiom1m1HXfC4D7dAAAIwzsipb4173.png "style=" float : none; "title=" Qq20170910190845.png "alt=" Wkiom1m1hxfc4d7daaaiwzsipb4173.png "/>

For example: [[email protected] bin]# head-n 1 </etc/passwd | Sed ' s,root,root,i ' root:x:0:0:root:/root:/bin/bash[[email protected] bin]# head-n 1 </etc/passwd | Sed ' s,root,root,gi ' root:x:0:0:root:/root:/bin/bash

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/06/42/wKiom1m1HXjgf8xzAAAILz07z44835.png "style=" float : none; "title=" Qq20170910190851.png "alt=" Wkiom1m1hxjgf8xzaaailz07z44835.png "/>

Example: [[email protected] bin]# Cat </etc/passwd | Sed-n ' S,root,root,ip ' Root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A4/F3/wKioL1m1HVOAfcKqAAAPbfJsYGQ821.png "style=" float : none; "title=" Qq20170910190857.png "alt=" Wkiol1m1hvoafckqaaapbfjsygq821.png "/>

# sed ' s,,, w/path/to/somefile ' file# sed ' s,,, ' FILE >/pth/to/somefile


Exercise 1: Remove any whitespace characters from the beginning of a line beginning with whitespace in the/boot/grub/grub.conf text

Exercise 2: Remove all # and white space characters from the beginning of the line in the/etc/fstab file that begin with #, followed by at least one white space character

Practice 3:echo an absolute path to the SED command: Take out its base name, remove the directory name


Exercise 1: Remove any whitespace characters from the beginning of a line beginning with whitespace in the/boot/grub/grub.conf text

CentOS 6: # cp/boot/grub/grub.conf/tmp/grub.conf # sed-i ' s,^[[:space:]]\+,, '/tmp/grub.confcentos 7: # Cp/et C/grub2.cfg/tmp/grub2.cfg # sed-i ' s,^[[:space:]]\+,, '/tmp/grub2.cfg

Exercise 2: Remove all # and white space characters from the beginning of the line in the/etc/fstab file that begin with #, followed by at least one white space character

# cp/etc/fstab/tmp/a.file# Sed-i-R ' s|^#[[:space:]]+| | '/tmp/a.file extension: Remove the line beginning with #, followed by no white space character, # # CP/ETC/FSTAB/TMP/FST ab# echo "#hello sed" >>/tmp/fstab# sed-i-R ' [Email protected]^# ([^[:space:]].*] @\[email protected] '/etc/fstab


Practice 3:echo an absolute path to the SED command: Take out its base name, remove the directory name

The test takes out the absolute path in the file name that exists Useradd # Locate-b useradd.../usr/share/man/zh_cn/man8/useradd.8.gz/usr/share/man/zh_tw/man8/ Useradd.8.gz ... Remove/usr/share/man/zh_tw/man8/useradd.8.gz's base name # echo "/usr/share/man/zh_tw/man8/useradd.8.gz/" |  Sed ' s,/$,, ' | Sed-r ' s# (. */) ([^/]+$) #\2# ' Remove the/usr/share/man/zh_tw/man8/useradd.8.gz directory name # echo "/usr/share/man/zh_tw/man8/ Useradd.8.gz "|  Sed ' s,/$,, ' | Sed-r ' s# (. */) ([^/]+$) #\1# '


format three :

Sed ' address bound command, address bound command, address bound command, ... '

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/42/wKiom1m1G6GxmPDuAABrv2sac9E011.png "title=" Qq20170910190112.png "alt=" Wkiom1m1g6gxmpduaabrv2sac9e011.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/A4/F3/wKioL1m1G53w-f3NAABOi48EL_0841.png "title=" Qq20170910190144.png "alt=" Wkiol1m1g53w-f3naaboi48el_0841.png "/>


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1964161

The use of the SED tool---------Linux

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.