File Processing Tool Series (ii): line editor SED

Source: Internet
Author: User
Tags line editor

1. Introduction to SED

Sed (Stream editor) is a line editing tool that processes a single line of content at a time. Processing, the current match to the row is stored in a temporary buffer, known as the "pattern space", and then use the SED command to make corresponding additions and deletions to the data, the processing is completed, the default output to the screen, and the line is not matched to the default output to the screen. The original file content does not change unless you use redirection to store the output.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/9C/wKiom1ep1dbTNvPcAAAMsKrIBe4763.png "title=" Sed.png "alt=" Wkiom1ep1dbtnvpcaaamskribe4763.png "/>

2. SED usage

<1> Syntax format

sed [option] ... ' Addresscommand ' Inputfile ...

<2>[option]

-N No output mode space and unmatched data
-E Multi-point editing
-R Support for using extended regular expressions
-I. In situ edit

Note: Although/tmp/fstab files can be processed in a similar manner through Sed-i.bak ' 4, $d '/tmp/fstab, and Fstab.bak backed-up files are generated in the/tmp directory, it is still not recommended to use in production life

[Email protected] ~]# Sed-i.bak ' 4, $d '/tmp/fstab [[email protected] ~]# Ls/tmp/fstab Fstab.bak functions

<3>address

Do not give address Search the full text
Single Address
# The specified row
/pattern/ Each row that is matched by the pattern
Address range
#,# The specified row to the specified row
#,+# The specified line to the next few lines
/pat1/,/pat2/ The first row that is matched by a pattern to the last row to which a pattern is matched
#,/pat1/ Rows to the last line to be matched by a pattern
Step in
A Odd rows
2~2 Even rows

<4>command

D: Delete the row to which the pattern space matches

P: Display rows in the mode space

Note: This command is generally used with the-n option, otherwise the match to the content will be displayed two times

A \text\: Append text after line, support multi-row append using \ n

I \text\: inserting text in front of the line, enabling multi-row insertion using \ n

C \text\: Replace with single or multiple lines of text

W/path/to/somefile: The save pattern matches the line to the specified file, and is generally used with-n

[Email protected] ~]# sed-n '/uuid/w/tmp/fstab.tmp '/etc/fstab[[email protected] ~]# cat/tmp/fstab.tmp uuid=cb86b5f6-3 Ea1-48b6-ad1d-5a77cf565d5b/boot XFS Defaults 0 0

R/path/from/somefile

[[email protected] ~]# cat ahaha[[email protected] ~]# sed '/kernel/r a '/etc/issue\skernel \ R on a \mhaha

s///: Find replacement, support use of other separators, [email protected]@@,s###

G: Global Substitution


For the above knowledge points, do the following strengthening exercises

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

[Email protected] ~]# sed-r ' [email protected]^[[:space:]]\[email protected]@ '/etc/grub2.cfg

2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file

[Email protected] ~]# sed-r ' [email protected]^#[[:space:]][email protected]@ '/etc/fstab

3. Add # At the beginning of each line of/etc/fstab

[[Email protected] ~]# sed ' [email protected]^@#&@ '/etc/fstab

Or

[[Email protected] ~]# sed ' [email protected]^.*@#&@ '/etc/fstab

Note: This cannot be written as sed ' [email protected]^ (. *) @#[email protected] '/etc/fstab, which is wrong, anchored here is (), not the beginning of the line

[email protected] ~]# cat > F1 () ab^c[[email protected] ~]# sed ' [email protected]^ (. *) @#&@ ' f1# () AB

4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #

[[Email protected] ~]# sed ' [email protected]^[^#]@#&@ '/etc/fstab

5, processing/etc/fstab path, use sed command to take out its directory name and base name

[Email protected] ~]# Echo/etc/fstab | Sed-r ' [email protected] (. */) ([^/]+/?$) @\[email protected] '/etc/[[email protected] ~]# Echo/etc/fstab | Sed-r ' [email protected] (. */) ([^/]+/?$) @\[email protected] ' Fstab

6. Use SED to remove the IPv4 address of the ifconfig command

[Email protected] ~]# Ifconfig | Sed-n ' 2p ' | Sed-e ' [email protected]*inet[[:space:]]@@ '-e ' [email protected][[:space:]].*@@ '

Or

[Email protected] ~]# Ifconfig | Sed-n ' 2p ' | Sed-r ' [email protected]*inet[[:space:]] (. *) [[: Space:]]net.*@\[email protected] ' 10.1.0.17

Of course, it's a lot easier if you use cut.

[Email protected] ~]# Ifconfig | Sed-n ' 2p ' | Tr-s ' | Cut-d '-f310.1.0.17

7. Statistics of all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field

[[email protected] packages]# ls | grep "rpm$" | Sed-e ' [Email protected][email protected]@ '-e ' [email protected]*\[email protected]@ ' | Sort | Uniq-c i686 2938 noarch 4069 x86_64

Or

[[email protected] packages]# ls | grep "rpm$" | Sed-r ' [email protected]*\. (. *) \[email protected]\[email protected] ' | Sort | Uniq-c i686 2938 noarch 4069 x86_64

Pay attention to common errors

Use regular expressions to note the escape of the.

Use extended Regular expressions to note when grouping to add-r

Again or

[[email protected] packages]# ls | grep "rpm$" | Rev | cut-d.-f2 | Sort | Uniq-c 4069 46_68x 686i 2938 Hcraon

Summary: Personally, the SED command is an extension of the grep command, although some functions, such as filtering the same rows , can be implemented by both, but if the amount of data is large, grep is more convenient. So grep filters the data, and the SED processes the data, which is undoubtedly the best result. As for the cut, TR and other commands, can also handle a lot of grep and SED to deal with the problem, such as filtering specific fields , so is very flexible, to practice, to master the ease.


This article is from the "dmwing" blog, make sure to keep this source http://dmwing.blog.51cto.com/11607397/1836436

File Processing Tool Series (ii): line editor SED

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.