The SED command for Linux Foundation

Source: Internet
Author: User
Tags uuid

The SED command is used to process the text, and it is to process the text in line, while the SED handles the text in the pattern space, the text content in the output mode space after processing, so if you use the SED command to process a file, it will not change the contents of the file itself, the change is only the temporary content of the pattern space.

Usage
‘AddressCommand‘...

Address: Can be understood as positioning data, through a series of actions to filter out the required data
Command: What to do with the selected data (rows)

Here, separately.

Address:    1、StartLine,EndLine        例:        1,100   --就是1到100行的意思        $:      --最后一行    2、/RegExp/        通过正则表达式来匹配        例:        /^pas/        --以pas开头的行    3、/pattern1/,/pattern2/         第一次被pattern1匹配到行开始,至第一次被pattern2匹配到的行结束,这中间的所有行    4、LineNumber        指定的行    5、StartLine, +N        从startLine开始,向后的N行
 Command:     d:删除符合条件的行     p:显示符合条件的行     a \string:在指定的行后面追加新行,内容为string     i \string:在指定的行前面追加新行,内容为string     r FILE: 将指定的文件的内容添加至符合条件的行处     w FILE: 将地址指定的范围内的行另存至指定的文件中;      s/pattern/string/修饰符:查找并替换,默认只替换每行中第一次被模式匹配到的字符串         加修饰符            g: 全局替换            i: 查找时忽略字符大小写     s///: s###, [email protected]@@   --使用s时,不仅可以用/来做分隔符,还可以使用#和@     \(\), \1, \2    --支持后向引用     &: 引用模式匹配到的整个字符串

Example: Take/etc/fstab file as an example

##/etc/fstab# Created by Anaconda on Mon 16:27:41## 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=e76a7b8d- -c2-4F94-BDD1-F4054A34C206/EXT4 defaults,barrier=0 1 1TMPFS/DEV/SHM TMPFS Defaults0 0Devpts/dev/pts devpts gid=5, mode=620  0 0Sysfs/sys SYSFS Defaults0 0PROC/PROC proc Defaults0 0

Delete First 10 rows

[root@iZ28g26851kZ‘1,10d‘ /etc/fstab devpts                  /dev/pts                devpts  gid=5,mode=620  00sysfs                   /sys                    sysfs   defaults        00proc                    /proc                   proc    defaults        00[root@iZ28g26851kZ

Delete lines that begin with the # sign

[Root@iZ28g26851kZ~]# SED'/^#/d '/etc/fstab uuid=e76a7b8d- -c2-4F94-BDD1-F4054A34C206/EXT4 defaults,barrier=0 1 1TMPFS/DEV/SHM TMPFS Defaults0 0Devpts/dev/pts devpts gid=5, mode=620  0 0Sysfs/sys SYSFS Defaults0 0proc/proc                   procDefaults0 0[Root@iZ28g26851kZ~]#

Example: Displaying lines beginning with the # number

[[Email protected] ~]# SED'/^#/p '/etc/fstab###/etc/fstab#/etc/fstab# Created by Anaconda on Mon 16:27:41# Created by Anaconda on Mon 16:27:41### Accessible filesystems, by reference, is maintained under '/dev/disk '# 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# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info##uuid=e76a7b8d- -c2-4F94-BDD1-F4054A34C206/EXT4 defaults,barrier=0 1 1TMPFS/DEV/SHM TMPFS Defaults0 0Devpts/dev/pts devpts gid=5, mode=620  0 0Sysfs/sys SYSFS Defaults0 0PROC/PROC proc Defaults0 0[Email protected] ~]#

Actually showed up 2 times, not only that, no match to the line is also shown,

Why is it?

As mentioned before, the SED process text is first read into the pattern space, after processing in the display, before the deletion, sed to match the matching results in the pattern space to delete the row, and then show the remaining lines in the pattern space is not a problem, but now, SED is the match to the results displayed, But at the same time also to display the contents of the pattern space, so there is such an embarrassing situation ~ ~

How to solve it?

-N

Use the -n command to turn on silent mode, which means that the contents of the pattern space are not displayed, but only the matching results are displayed.

‘/^#/p‘## /etc/fstab# Created by anaconda on Mon Aug 11 16:27:41 2014## 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#

Example: Add a Line "test" to all the # numbers

[[Email protected] ~]# SED'/^#/a \test '/etc/fstab#Test#/etc/fstabTest# Created by Anaconda on Mon 16:27:41Test#Test# Accessible filesystems, by reference, is maintained under '/dev/disk 'Test# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more infoTest#testuuid=e76a7b8d- -c2-4F94-BDD1-F4054A34C206/EXT4 defaults,barrier=0 1 1TMPFS/DEV/SHM TMPFS Defaults0 0Devpts/dev/pts devpts gid=5, mode=620  0 0Sysfs/sys SYSFS Defaults0 0PROC/PROC proc Defaults0 0[Email protected] ~]#

Example: Saving a line that starts with a # number in a test10.txt file

[[Email protected] ~]# sed '/^#/w test10.txt '/etc/fstab##/etc/fstab# Created by Anaconda on Mon 16:27:41## 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=e76a7b8d- -c2-4F94-BDD1-F4054A34C206/EXT4 defaults,barrier=0 1 1TMPFS/DEV/SHM TMPFS Defaults0 0Devpts/dev/pts devpts gid=5, mode=620  0 0Sysfs/sys SYSFS Defaults0 0PROC/PROC proc Defaults0 0[[Email protected] ~]# lsExittest. SHlibiconv-1.14mhash-0.9. 4. Tar. GZmysql-5.1. Wuyi. Tar. GZnginx-1.8. 0. Tar. GZTest10. txtFiletest. SHlibiconv-1.14. Tar. GZmysql-5.1. Wuyinginx-1.8. 0Shifttest. SHVartest. SH[[Email protected] ~]# cat Test10.txt##/etc/fstab# Created by Anaconda on Mon 16:27:41## 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] ~]# 

Example: replacing all/Replace with "@[email protected]"

[Root@iZ28g26851kZ~]# sed ' s#/#@O@ #g '/etc/fstab## @O@etc@O@fstab # Created by Anaconda on Mon 16:27:41## Accessible filesystems, by reference, is maintained under '@O@dev@O@disk' # See mans Pages Fstab (5), Findfs (8), Mount (8) and@O@or blkid (8) for more info#UUID=e76a7b8d- -c2-4f94-bdd1-f4054a34c206@O@ ext4 defaults,barrier=0 1 1Tmpfs@O@dev@O@shmTMPFS defaults0 0Devpts@O@dev@O@ptsDevpts gid=5, mode=620  0 0Sysfs@O@sysSYSFS defaults0 0Proc@O@procProc Defaults0 0[Root@iZ28g26851kZ~]# 
-I.

It was previously said that all operations of the SED are for pattern space and do not affect the source file.
What if it's like modifying the source file?
Then use the-I option
This is the direct operation of the source files, so carefully use ~ ~

-E

Execute multiple scripts at the same time

‘AddressComment‘‘AddressComment‘...
-F

Write the script to a file, each behavior a script, and then,,,

...
-R

Supports extended regular expressions

======================================
Example: Finding the directory of files from the file directory

[root@iZ28g26851kZ ~]# echo "/test/test2/etc/passwd" | sed -r ‘[email protected]^(/.*/).+/[email protected]\[email protected]‘ /test/test2/etc/[root@iZ28g26851kZ ~]# 

Example: Finding the file name from the files directory

[root@iZ28g26851kZ ~]# echo "/test/test2/etc/passwd" | sed -r ‘[email protected]^(/.*/)(.+)/[email protected]\[email protected]‘ passwd[root@iZ28g26851kZ ~]# 

The SED command for Linux Foundation

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.