Sed command introduction, sed command

Source: Internet
Author: User

Sed command introduction, sed command

Sed

It is a stream editor that processes a row of content at a time. during processing, the currently processed row is stored in a temporary buffer, which is called a mode space. Then, the sed command is used to process the content in the mode space, after processing, the buffer content is sent to the screen. Then read the data to the downstream and execute the next loop.

 

Usage: sed [option]... 'script' inputfile...

#: Specify a line. Otherwise, the full text will be displayed.
/Pattern/: each row that can be matched by this mode
#,#: Specified range
#, + #: Specify # Rows to # + N rows
/Pat1/,/pat2/
#,/Pat1/

Common options:
-N: No output mode space content to the screen
-E: Multi-Point editing
-F:/PATH/script file: Read the editing script from the specified file.
-I. bak: Back up the original file and edit it.
-R: supports extended regular expressions.

D: Delete
Sed '/^ $/d': deletes blank rows.
Sed '1, 10d ': delete 1-10 rows (multi-line editing is available between multiple rows)

P: displays the content in the mode space.
Sed-n' 1 ~ 2 P 'file: displays only odd rows
Sed-n' 2 ~ 2 P 'file: displays only even rows

A [\] test: append text after a specified row. You can use \ n to append multiple rows.
Sed '/root/a \ superman'/etc/passwd: append a superman line after the row containing root

I [\] text: insert text before a row
Sed '/root/I \ superman'/etc/passwd: append superman to the first line of the row containing root

C [\] text: replace a single line or multiple lines of text
Sed '/root/c \ superman'/etc/passwd: Replace the contents of all rows containing root with superman.

W/path/file: Save the row matching the mode to the specified file.

R/path/file: after reading the text of the specified file to the matched row in the mode space

=: Print the row number for the row in the mode space
Sed-n-e '/^ $/p'-e'/^ $/= 'file: displays blank rows and prints the row number.

! : Reverse

S // s @ s ####: Search for replacement
G: Intra-row global replacement
P: display the replaced rows
W/PATH/file: Save the replaced rows to the file.

Advanced Editing Command
H: overwrite the content in the mode space to the reserved space.
H: append the content in the mode space to the reserved space.
G: extract data from the reserved space to overwrite the mode space.
G: append the content from the reserved space to the mode space.
X: swap the content in the pattern space with the content in the reserved space.
N: Read the next row of the matched row to overwrite the mode space.
N: Read the next row of the matched row and append it to the mode space.
D: delete rows in the mode space.
D: delete all rows in the multi-row mode space.

Example:

1. Delete all blank characters at the beginning of the line starting with blank in the centos7 system/etc/grub2.cfg File
Sed/^ [[: blank:]/D'/etc/grub2.cfg

 

2. delete all the lines starting with # in the/etc/fstab file, followed by at least one blank character.
Sed '/^ # [[: blank:] \ +/D'/etc/fstab

 

3. Add # At the beginning of each line of centos6 system/root/install. log
Sed-n's/^/# &/P'/root/install. log

 

4. In the/etc/fstab file, add the # sign at the beginning of the line that does not start #
Sed's/^ [^ #]/# &/'/etc/fstab

 

5. process the/etc/fstab path and use the sed command to retrieve the directory name and base name.
Directory Name: echo/etc/fstab | sed's @ [^/] \ + $ @@'

Base name: echo/etc/fstab | sed s '@.*/@@'

 

6. Use sed to retrieve the IPv4 address of the local machine in the ifconfig command
Ifconfig | sed-n'/inet addr/P' | sed's/^ [[: blank:] * inet addr: // '| cut-d'-f1

Ifconfig | sed-n '2p' | tr-s': | cut-d:-f4

 

7. count the number of times that all rpm files in the Package directory of the centos installation disc are duplicated in the Second Field separated.

Mount/dev/cdrom/mnt | ls/mnt/Packages/| sed s '/. rpm // '| sed s '@. * [/.] @ '| sort | uniq-c

 

8. Count and sort the occurrences of each word in the/etc/init. d/functions File

Sed s'/[^ [: alpha:]/\ n/G'/etc/init. d/functions | sort | uniq-c | sed '1d'


9. Merge n and n + 1 rows of the text file into one row, and n is an odd row.
Sed 'n'; s/\ N //'

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.