The SED command for Linux learning notes

Source: Internet
Author: User

To speak and be sensitive to the line--"The Analects of Confucius and Leezen"

The SED command is a text-processing tool that is used to process an input stream. Mastering it can better help manage Linux and process log files.

Usage:

sed [option] ' command ' input stream

Common option:

-N: Quiet mode, generally if you do not have the-n option, all rows in the input stream will be listed, and adding-N will only list the rows processed by ' command '. Normally with command P commands.

-r: The default SED only supports generic regular expressions, and if the-r parameter indicates that the SED supports extended regular expressions.

-I: INSERT, if the input stream is a file stream, modify the content directly in the file instead of outputting it to the screen. (use with caution)

Common ' command ':

A (i): Add (insert) to insert a string after the number of target rows (the previous line).

D:delete, delete the destination row.

P:print, print the target line.

S:substitute, replacing the target string with a new string. Usually this s action can be paired with a regular expression and is similar to a replacement in vim. 1,20s/old/new/g

C: Replace the target line

Example:

Delete:

# sed ' 1d ' #删除第一行
# sed ' $d ' #删除最后一行
# sed ' 1,10d ' #删除第一行到第十行
# sed ' 2, $d ' #删除第二行到最后一行

Show:

. # sed-n ' 1p ' #显示第一行
# sed-n ' $p ' #显示最后一行
# sed-n ' 1,2p ' #显示第一行到第二行
# sed-n ' 2, $p ' #显示第二行到最后一行

# sed–n '/dbird/p ' #打印所有包含dbird line

# sed–n '/\$/p ' #打印所有包含 $ sign Line

Insert:

#sed ' 1,3a drink tea #在1到3行后面插入drink tea, the drink tea is in the first 2,4,6.

#sed 1,3i Drink Tea #在1到3行之前插入drink tea, the drink tea was in the first 1,3,5.

Line substitution:

#sed ' 1,2c Hi ' #将第一到二行替换为Hi

String substitution:

Format: sed ' s/string to replace/new string/g ' (the string to replace can be used with regular expressions)
#sed ' s/dbird/vbird/g ' #替换dbird为vbird
#sed ' s/dbird//g ' #删除dbird

#sed-i '/Match string/d ' filename #直接删除文件filename中的匹配的字符串

The SED command for Linux learning notes

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.