Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sed command details

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Sed:

How it works: this command is also a command for document lines. He works in the same way as grep, the goal is to copy the document to his memory to operate, as long as the user does not save, the original file will not be changed.

Disk source file = A-> Source file or part of the original file copied to memory = B-> File saved in memory after operation, filtering or editing = C

Common options:

-n: indicates that the output is no longer consistent with the content from tunnel B, tunnel C.

-e: you can use multiple editing instructions to edit, filter and fabricate the content of place B at the same time.

-f: it can be followed by a script file, each line of which is an editing command

-r: supports the use of extended regular expressions

-I: it means to edit the original file directly. This option is dangerous.

Address delimiting: and indicates which lines we want to edit

1): no address, that is, an empty address. It means to process the full text.

2): single address

#: make a line

3): a range

#, #:

# . + #:

/ pat1/,/part2/

4) step:-

1-2: represents all odd lines

2-2: represents all even rows

Common editing options for sed

D: delete, that is, delete the content of "B"

Sed'1j 5d'/ etc/fstab means to delete 1 line and 5 lines.

Sed'/ ^ UUID/d' / etc/fstab deletes every line that begins with UUID

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

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

P: indicates that the content of "place B" is displayed

Sed '1lines 2p' / etc/fstab means odd lines are displayed twice.

Sed-n '1room2p' / etc/fstab means to show only odd lines-n means only the editing behavior is retained.

A\ text: append "text" to the line to which the specified pattern is matched. Support the use of\ nthis symbol to achieve multi-line append.

I\ text: insert "text" in front of the line to which the specified pattern matches. The symbol\ nis supported to implement multiline append.

For example: sed'5i\ do it yourself' / etc/fstab (I deleted the space in the first line of fstab, so it's the following effect)

# # / etc/fstab# Created by anaconda on Wed Dec 30 06:43:45 2015#do it yourself# 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

Sed'5a\ do it yourself' / etc/fstab

# # / etc/fstab# Created by anaconda on Wed Dec 30 06:43:45 2015 Accessible filesystems, by reference, are maintained under'/ dev/disk'do it yourself# See man 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 following actions

Sed'/ UUID/a\ # I will do my best.' / etc/fstab

# # / etc/fstab# Created by anaconda on Wed Dec 30 06:43:45 2015 March # 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 11 I will do my best.UUID=ec7cf699-9e55-4305-a463-859f8ba83f3b / boot ext4 defaults 12 I will do my best.UUID=52937bf3-72c9-48d7-a05c-72726731c995 swap swap defaults 0 0 will do my best.

You can see / UUID/=5

C\ text: replace the matching line with the text specified here. "text' Note: it is a positive line replacement, not just a character."

For example:

Sed'/ UUID/c\ # I will 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##I will do my best.#I will do my best.#I will do my best.

W / PATH/TO/FILE: save selected lines to a path, such as "Save lines that do not begin with the # sign in / tmp/temp"

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

[root@marco-linux temp] # cd / tmp/temp [root@marco-linux temp] # lsetc files6 new.xx passwd1 passwd2 passwd3 [root@marco-linux 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: for merging files ~! This option means that another file is read and stored on the line defined by the sed target file, for example, we read the / etc/isssue file and put it under 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 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 to each line that can be matched: sed'/ ^ UUID/='/ etc/fstab

9UUID=42725ac6-787d-4be4-a766-a4b9325eba7e / ext4 defaults 1 110UUID=ec7cf699-9e55-4305-a463-859f8ba83f3b / boot ext4 defaults 1 211UUID=52937bf3-72c9-48d7-a05c-72726731c995 swap swap defaults 0 0

For example, delete all lines that do not start with # in the fstab file.

Sed'/ ^ # /! D' / 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#

For example, the delimiter can be specified by itself, for example, sparking $sparking replacement @, etc.

Replace the tag:

G: global replacement

W: / PATH/TO/IILE: save the result of successful replacement to the specified file

P: show the rows that have been replaced successfully

For example: sed's @ ^ [[: space:]] @ @'/ PATH/TO/FILE is empty between the last two @ @

That's all sed said. I wish you a happy study, and I wish you a good time in my study.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report