sed command to elaborate

Source: Internet
Author: User

Sed:

How it works: This command is also a command for a document line. He works the same way as grep, where the work target is the line in the document, and the same thing is copying the document into his memory, as long as the user does not save it, and the original file is not changed.

The disk source files are =a to the source files in memory or to parts of the original file =b---> Operations, filtering, or editing of files saved in memory =c

Common options:

-N: Indicates no longer outputs the content that does not conform to the C ground from B

-E: You can use multiple editing instructions to edit, filter, and make the content of B

-F: Behind can be followed by a script file, this script file each line is an edit command

-R: Supports the use of extended regular expressions

-I: This is a dangerous option for editing the original file directly.


Address delimitation: and means we're going to edit those lines.

1): Do not give address, that is, empty address. The full text is processed

2): Single address

#: Make a line

3): a range

#,#:

#。 +#:

/pat1/,/part2/

4) Stepping:-

1-2: All odd lines are represented

2-2: All even lines are represented


sed common editing options

D: Delete the contents of "B place"

Sed ' 1,5d '/etc/fstab to delete 1 rows and 5 rows of content

Sed '/^uuid/d '/etc/fstab means to delete each line beginning with the UUID

Sed ' 3d '/etc/fstab means to delete the third row

Sed ' 1-2d '/etc/fstab indicates that even rows are displayed (because odd rows are deleted)

P: Indicates the content of "B-place" is displayed

Sed ' 1~2p '/etc/fstab means odd lines show two times

Sed-n ' 1~2p '/etc/fstab indicates that only odd rows are displayed-n means only the edited behavior is preserved

A\text: Appends the specified pattern to the line appended by "text", supporting the use of \ n This symbol for multi-line append

I\text: Insert "text" in front of the line to which the specified pattern matches, supporting the use of \ n This symbol for multi-line append

For example: sed ' 5i \do It Yourself '/etc/fstab (I deleted the first line of Fstab, so it is the effect below)

# #/etc/fstab# Created by Anaconda in Wed Dec 06:43:45 2015#do It yourself# Accessible filesystems, by reference, is M aintained under '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

SED ' 5a \do it Yourself '/etc/fstab

# #/etc/fstab# Created by Anaconda in Wed Dec 06:43:45 2015## Accessible filesystems, by reference, is maintained Unde R '/dev/disk ' do It yourself# See mans pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

If you want to insert information below or above multiple lines, you can use the action

Sed '/uuid/a \ #I'll do my best. '/etc/fstab

## /etc/fstab# created by anaconda on wed dec 30 06:43:45  2015## 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=42725ac6-787d-4be4-a766-a4b9325eba7e /                         ext4    defaults        1 1#i will  Do my best. uuid=ec7cf699-9e55-4305-a463-859f8ba83f3b /boot                    ext4    defaults         1 2#i will do my best. Uuid=52937bf3-72c9-48d7-a05c-72726731c995 swap                     swap    defaults         0 0#i will do my best.

Can see/uuid/=5


C \text: Replace the matched line with the text specified here "text" Note: is a positive line substitution, not just a character replacement.

For example:

Sed '/uuid/c \ #I'll do my best. '/etc/fstab

# #/etc/fstab# Created by Anaconda in Wed Dec 06:43:45 2015## Accessible filesystems, by reference, is maintained Unde R '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) For more info# #I would do my best. #I would do my Best #I'll do my best.

W/path/to/file: Save the selected row to a path, for example, "Save the line that is not the beginning of the # number in the/tmp/temp."

Sed-n '/^[^#]/w/tmp/temp/new.xx '/etc/fstab

[[email protected] temp]# cd /tmp/temp[[email protected] temp]# lsetc   files6  new.xx  passwd1  passwd2  passwd3[[email protected]  temp]# cat new.xxUUID=42725ac6-787d-4be4-a766-a4b9325eba7e /                         ext4    defaults        1 1uuid= ec7cf699-9e55-4305-a463-859f8ba83f3b /boot                    ext4    defaults         1 2UUID=52937bf3-72c9-48d7-a05c-72726731c995 swap                     swap     defaults        0 0tmpfs                    /dev/shm                 tmpfs   defaults         0 0devpts                   /dev/pts                 devpts  gid=5,mode=620  0 0sysfs                    /sys                      sysfs   defaults        0 0proc                     /proc                    proc     defaults        0 0

R/path/from/somefile: Merge files with! This option means that another file is read and stored at the line of the SED target file. For example, we read the/etc/isssue file and put it in the third line of the Fstab file.

Sed ' 3r/etc/issue '/etc/fstab

## /etc/fstabcentos release 6.7  (Final) kernel \r on an \m#  Created by anaconda on wed dec 30 06:43:45 2015## 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=42725ac6-787d-4be4-a766-a4b9325eba7e /                        ext4     defaults        1 1uuid= ec7cf699-9e55-4305-a463-859f8ba83f3b /boot                    ext4    defaults         1 2uuid=52937bf3-72c9-48d7-a05c-72726731c995 swap                     swap    defaults         0 0tmpfs                    /dev/shm                 tmpfs   defaults        0  0devpts                   /dev/pts                 devpts  gid=5,mode=620  0 0sysfs                    /sys                     sysfs   defaults         0 0proc                     /proc                    proc    defaults         0 0

=: Add a line number for each line that can be matched: sed '/^uuid/= '/etc/fstab

9UUID=42725AC6-787D-4BE4-A766-A4B9325EBA7E/EXT4 Defaults 1 110uuid=ec7cf699-9e55-4305-a4                    63-859f8ba83f3b/boot ext4 defaults 1 211uuid=52937bf3-72c9-48d7-a05c-72726731c995 swap Swap Defaults 0 0

! : To remove all lines that begin with a #, for example, in the Fstab file.

Sed '/^#/!d '/etc/fstab

# #/etc/fstab# Created by Anaconda in Wed Dec 06:43:45 2015## Accessible filesystems, by reference, is maintained Unde R '/dev/disk ' # See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info#

s///: Find replacement, its delimiter can be specified, such as s$$$ [email protected]@@@, etc.

Replace tag:

G: Global Substitution

W:/path/to/iile: Saves the result of the substitution success to the specified file

P: Show the rows that were successfully replaced

For example: sed ' [email protected]^[[:space:]]@@ '/path/to/file after two @@ represents an empty


Sed says these things. Wish you to study happily, also wish me to study happily

sed command to elaborate

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.