The-sed of the Three Musketeers in text processing

Source: Internet
Author: User

1. Text Processing Three Musketeers-sed

#sed语法: sed [OPTION] ...       {Script-only-if-no-other-script} [Input-file] ...

Sed understanding: Is a stream editor, processing one line at a time, processing the currently processed rows stored in the temporary cache, become "pattern space", and then use the SED command to process the contents of the buffer, after processing the buffer content output to the screen and then processing the next line. This keeps repeating to the end. The contents of the file did not change. The content will not change unless you use the redirected output.

Common Options

-N does not output mode space to screen-e multi-point editing, or-F reads edit script from specified file-R uses extended expression-i-place edit


2.sed Processing address range delimitation:

1. Do not give address to the full text processing


2. Single address specifies line ' 1 ' first row;

/keyword, can be a regular expression or other fuzzy matching symbol/


3. Address range

' 2,10 ' second line to line tenth

' 2,+10 ' second line plus 10 lines

/keyword/,/keyword/match the content between the first keyword and the second keyword;


4. Step in

' A ' or ' odd ' line

' 2~2 ' even lines


3.sed Editing commands

D Delete the row to which the pattern space matches

[[Email protected] testdir]# sed ' 1d ' 1 * 3#/etc/fstab 4# Created by Anaconda on Thu Jul 28 17:47:00 2016


P Display the contents of the mode space

[Email protected] ~]# sed ' 2p '/etc/issue\skernel \ r on an \mkernel \ R on an \m

Displayed two times because in the mode space output once and then P is displayed once, plus-n is a non-output mode space to the screen, the following output


[Email protected] ~]# sed ' 2p '/etc/issue\skernel \ r on an \m


A \ Add content after matching lines add text \ n Multiple lines insert

#实例解释 [[Email protected] testdir]# sed -e  /uuid/a\ Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -e /uuid/i\zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz   file1     1     2#      3# /etc/fstab     4# created by anaconda on thu  jul 28 17:47:00 2016     5#     6#  accessible filesystems, by reference, are maintained under  '/dev/disk '      7# see man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info     8#      9/dev/mapper/centos-root /                        xfs     defaults        0  0zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz    10uuid=aea23617-8678-414a-bd12-59f03ea9deb0  /boot                    xfs     defaults        0  0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


I \ Add content after matching lines add text \ n Multiple lines insert

#实例解释 [[email protected] testdir]# sed '/^u/i\zzzzzzzzzzzzzzzzzzzzz\nnnnnnnnnnnnnnnnnnnnnnn\nssssssssssssssss ' file3 #输出结果ZZZZZZZZZZZZZZZZZZZZZNNNNNNNNNNNNNNNNNNNNNNssssssssssssssssUUID =aea23617-8678-414a-bd12-59f03ea9deb0/boot



C \ Replace content replaces single-line or multiline text

#实例解释, also supports \ n line break [[email protected] testdir]# sed '/^u/c\relace ' file3# replace line with ' Relace '


w/Save new file path

[[Email protected] testdir]# sed '/^u/w/testdir/5 ' file3


R/Save File path

[[Email protected] testdir]# sed '/^u/r/etc/issue ' file3


= Print line number in pattern space

[[Email protected] testdir]# sed '/^u/= ' file6


! Inverse of pattern space matching

[[Email protected] testdir]# sed '/^u/!= ' file6


#ps: SED file does not modify the contents of the original file, if you want to modify the can use the option-I

[Email protected] testdir]# sed-i '/^u/d ' 3

You can back up files before you modify them, and you can back up the file before you modify it by adding a backup name after the-i option

[Email protected] testdir]# sed-i.bak '/^u/d ' 3


4.sed Search Replacement

s///find replacement support using the delimiter includes the [email protected]@@,s### replacement tag; g: Global substitution P: Display substitution successful row W saves the replacement row to a new file
#实例操作解释 # #, delete all whitespace characters in the/etc/grub2.conf file that begin with the line beginning with whitespace [[email protected] testdir]# sed  '/^[[:space :]]\+/d '  /etc/grub2.cfg  #2, delete all # and white space characters at the beginning of the line beginning with # in the/etc/fstab file, followed by at least one white-space character [[email protected]  testdir]# sed  ' s/^#[[:space:]]\+//'  /etc/fstab#3, add # [[email  at the beginning of each line at/root/install.log protected] ~]# sed -n  ' s/^/#/p '  /root/install.log#4, add # At the beginning of the line in the/etc/fstab file that does not start with # [[ email protected] ~]# sed  ' s/^[^#]/#/'  /etc/fstab#5, processing/etc/fstab path, Use the SED command to take out its directory name and base name echo  "/ETC/FST/SD"  | sed -r   ' [email protected] (. *)/([^/ ]+/?) @\[email protected] ' echo  "/etc/fst/sd"  | sed   -r  ' [email  Protected] (. */) ([^/]+/?) [email protected]\[email protected] '  echo  '/etc/fst/sd/'  | sed   ' [ email protected]*\<@@ ' fetch directory echo  '/etc/fst/sd/'  | sed -r   ' s#[^/]+/?$## ' #6, Use sed  to remove ifconfIPV4 address of this machine in IG command [[email protected] ~]# ifconfig|sed -n  ' 2p ' |sed  ' s/\<b.*//' |sed   ' s/^.*r: '//#7, count all rpm files in the package directory on the CentOS installation CD-ROM to separate the number of repetitions of the second-to-last field [[email protected] packages]#  ls |sed  ' s/.rpm$ '//|sed  ' s/.*\. ' |sort|uniq -c



5.sed Advanced Applications

The first thing to know is that using sed two cache space, one is the pattern space, as mentioned in the previous

The other is to keep the space

The following are some of the advanced applications of SED

H: Overwrite the contents of the pattern space in the hold space

H: Append the contents of the pattern space to the hold space

G: Remove data from hold space to pattern space

G: Append content from hold space to mode space

x: Swap the content in the pattern space with the content in the hold space

N: Reads the next line of the matched row to overwrite the pattern space

N: Append the next line of matching rows to the mode space

D: Delete rows in the pattern space

D: Delete the contents of the current mode space beginning to \ n (not passed to standard output), discard the following command, but re-execute sed on the remaining mode space


#实例解释以后补充


The-sed of the Three Musketeers in text processing

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.